rm(list=ls()) 
setwd("")#working directory
options(scipen=999) 
options(max.print=1000000)

install.packages("readr")
install.packages("ggplot2")
install.packages("translateR")
install.packages("stringr")
install.packages("splitstackshape")
install.packages("cowplot")
install.packages("dplyr")
install.packages('Jmisc')
install.packages('psych')
install.packages('qwraps2')
install.packages("multiwayvcov")
install.packages("papeR")
install.packages("effects")


library(translateR);library(readr);library(ggplot2);library(stringr);library(plyr);
library(splitstackshape);library(data.table);library(dplyr);library(cowplot);library(Jmisc);library(psych);library(qwraps2);library(xtable);library(sandwich);library(stargazer);library(lmtest);library(ggpubr);library(multiwayvcov);library(freqtables);library(fastDummies)
library(wordcloud);library(stm);library(readxl)
library(broom);library(purrr);library(ggpubr);library(papeR);library(readr);library(REdaS);library(freqtables);library(fastDummies)
library(papeR);library(ggeffects);library(effects)

##load data
##############################################################################################################
final <- read_csv("data_file1.csv")
final$my_station <- as.factor(final$my_station)
final <- data.frame(final)
data.frame(colnames(final))


###########
names(final)
##Each FIR can have multiple hearings. Restrict only to individual FIRs first
data3 <- select(final, 1:103)
data3 <- unique(data3)

##
data3$complainant_gender <- as.factor(data3$complainant_gender)
data5 <- select(data3, complainant_gender, word_count, distance, diff_in_days, female_suspect_count, total_suspects, victim_age, no_of_sections, io_rank, ps_category2, diff_in_hours, hour_of_registration, hour_of_arrival)

##
data5 <- dummy_cols(data5, select_columns = "io_rank")
descriptives1 <- xtable(summarize(data5, type = "numeric", group = "complainant_gender"))
names(descriptives1)
descriptives1 <- descriptives1[,c(1,2,4,7,8)]
print(xtable(descriptives1), include.rownames=FALSE)

##
test <- xtable(summarize(data5))
test <- test[,c(1,2,5,6,10)]
print(xtable(test), include.rownames=FALSE)

##
data3$gendered <- as.factor(data3$gendered)
data5 <- select(data3, gendered, complainant_gender,  word_count, distance, diff_in_days, female_suspect_count, total_suspects, victim_age, no_of_sections, io_rank, ps_category2, diff_in_hours, hour_of_registration, hour_of_arrival)

##
data5 <- dummy_cols(data5, select_columns = "io_rank")
descriptives2 <- xtable(summarize(data5, type = "numeric", group = "gendered"))
names(descriptives2)
descriptives2 <- descriptives2[,c(1,2,4,7,8)]
print(xtable(descriptives2), include.rownames=FALSE)

##
names(descriptives1)[1] <- "variable"
descriptives2 <- select(descriptives2, 2:5)
descriptives <- cbind(descriptives1, descriptives2)
print(xtable(descriptives), include.rownames=FALSE)

##Table 1
names(test)[1] <- "variable"
p <- plyr::join(descriptives, test, type='left', by='variable')
print(xtable(p), include.rownames=FALSE)

###################################
## 
women <- data3[with(data3, grepl("female", data3$complainant_gender)),]
degree <- women[, 67:103]
names(women)

#
test <- xtable(summarize(degree, digits = 4))
test <- test[order(-test$Mean),]
test <- test[1:20,]

#
list <- test
colnames(list)[1] <- "a"
list <- dplyr::select(list, a, Mean)

list$type <- c("intimidation","voluntarily causing hurt", "act done by several persons", "dowry harassment/cruelty by husband or relatives", "breach of trust", "theft", "burglary", "wrongful confinement/missing person", "trespassing/preparation for hurt or assault", "cheating","criminal force to woman with intent to outrage her modesty","rash driving", "trespassing by night", "causing hurt", "unlawful assembly", "wrongful restraint","kidnapping", "rioting, armed with a weapon","criminal conspiracy", "mischief, causing damage")
list$type2 <- c("IPC 1860;506","IPC 1860;323", "IPC 1860;34", "IPC 1860;498-A", "IPC 1860;406", "IPC 1860;379", "IPC 1860;380", "IPC 1860;346", "IPC 1860;452", "IPC 1860;420","IPC 1860;354","IPC 1860;279", "IPC 1860;457", "IPC 1860;337", "IPC 1860;149", "IPC 1860;341","IPC 1860;365", "IPC 1860;148","IPC 1860;120-B", "IPC 1860;427")

women_cases <- ggplot(data = list, aes(reorder(type2, desc(-Mean)), y = Mean)) + geom_bar(stat="identity") + coord_flip() + ggtitle("Top 20 Penal Codes [Female Complainants]") 
women_cases <- women_cases + xlab("Indian Penal Code (IPC)") + ylab("Proportion") 
women_cases <- women_cases + theme(plot.title = element_text(size = 15, hjust=0.5)) 
women_cases <- women_cases + scale_y_continuous(breaks = seq(0, .40, .05), limits=c(0, .40)) + theme(text = element_text(size = 20)) 
women_cases <- women_cases + geom_text(aes(label = type), hjust=-0.04, position = position_dodge(0.5))
women_cases

##
men <- data3[with(data3, grepl("other", data3$complainant_gender)),]
degree <- men[, 67:103]

#
test <- xtable(summarize(degree, digits = 4))
test <- test[order(-test$Mean),]
test <- test[1:20,]

#
list <- test
colnames(list)[1] <- "a"
list <- dplyr::select(list, a, Mean)

list$type <- c("theft","rash driving", "intimidation", "voluntarily causing hurt", "offenses related to illicit liquor", "burglary", "acts done by several persons", "causing hurt", "trespassing by night", "cheating", "causing death by negligence", "non-appearence in response to order", "offenses related to illicit liquor", "wrongful confinement/missing person", "unlawful assembly", "offenses related to illicit liquor", "offenses related to arms/ammunition", "wrongful restraint", "mischief, causing damage", "rioting, armed with a weapon")
list$type2 <- c("IPC 1860;379","IPC 1860;279", "IPC 1860;506", "IPC 1860;323", "PUNJAB EXCISE ACT 1914;68", "IPC 1860;380", "IPC 1860;34", "IPC 1860;337", "IPC 1860;457", "IPC 1860;420", "IPC 1860;304-A", "IPC 1860;174-A", "PUNJAB EXCISE ACT 1914;1", "IPC 1860;346", "IPC 1860;149", "PUNJAB EXCISE ACT 1914;14", "ARMS ACT 1959;25", "IPC 1860;341", "IPC 1860;427", "IPC 1860;148")

men_cases <- ggplot(data = list, aes(reorder(type2, desc(-Mean)), y = Mean)) + geom_bar(stat="identity") + coord_flip() + ggtitle("Top 20 Penal Codes [Male Complainants]") 
men_cases <- men_cases + xlab("Indian Penal Code (IPC)") + ylab("Proportion") 
men_cases <- men_cases + theme(plot.title = element_text(size = 15, hjust=0.5)) 
men_cases <- men_cases + scale_y_continuous(breaks = seq(0, .25, .05), limits=c(0, .25)) + theme(text = element_text(size = 20)) 
men_cases <- men_cases + geom_text(aes(label = type), hjust=-0.04, position = position_dodge(0.5))
men_cases#Figure A1

## 
gendered <- data3[with(data3, grepl(1, data3$gendered)),]
degree <- gendered[, 67:103]

#
test <- xtable(summarize(degree, digits = 5))
test <- test[order(-test$Mean),]
test <- test[1:20,]

#
list <- test
colnames(list)[1] <- "a"
list <- dplyr::select(list, a, Mean)

list$type <- c("intimidation","voluntarily causing hurt", "dowry harassment/cruelty by husband or relatives", "acts done by several persons", "breach of trust", "criminal force with intent to outrage her modesty","kidnapping a woman to compel her to marriage", "kidnapping from guardianship", "procuration of minor girl", "obscene acts/songs", "trespassing/preparation for hurt or assault", "rape", "criminal conspiracy", "word, gesture or act intended to insult modesty of woman", "dowry death", "stalking", "wrongful restraint", "sexual harassment", "unnatural (anal) sex", "unlawful assembling")
list$type2 <- c("IPC 1860;506","IPC 1860;323", "IPC 1860;498-A", "IPC 1860;34", "IPC 1860;406", "IPC 1860;354", "IPC 1860;366", "IPC 1860;363", "IPC 1860;366-A", "IPC 1860;294", "IPC 1860;452", "IPC 1860;376", "IPC 1860;120-B", "IPC 1860;509", "IPC 1860;304-B", "IPC 1860;354-D", "IPC 1860;341", "IPC 1860;354-B", "IPC 1860;377", "IPC 1860;149")

gendered_cases <- ggplot(data = list, aes(reorder(type2, desc(-Mean)), y = Mean)) + geom_bar(stat="identity") + coord_flip() + ggtitle("Top 20 Penal Codes [VAW]") 
gendered_cases <- gendered_cases + xlab("Indian Penal Code (IPC)") + ylab("Proportion") 
gendered_cases <- gendered_cases + theme(plot.title = element_text(size = 15, hjust=0.5)) 
gendered_cases <- gendered_cases + scale_y_continuous(breaks = seq(0, .90, .1), limits=c(0, .90)) + theme(text = element_text(size = 20)) 
gendered_cases <- gendered_cases + geom_text(aes(label = type), hjust=-0.04, position = position_dodge(0.5))
gendered_cases

##Figure A2
ggarrange(women_cases, gendered_cases, ncol=2, nrow=1, labels = c("A)", "B)"))


#
plot2 <- ggplot(data3, aes(x=complainant_gender, y=word_count)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 3000)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                  position=position_jitter(width=0.1, height=0.2)) 
plot2 <- plot2 + xlab("Complainant Gender") + ylab("Word Count") 
plot2 <- plot2 + theme_minimal() + ggtitle("Word Count of FIR by Complainant Gender") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
plot2

#
plot22 <- ggplot(data3, aes(x=gendered, y=word_count)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 3000)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                  position=position_jitter(width=0.1, height=0.2)) 
plot22 <- plot22 + xlab("Crime Type") + ylab("Word Count") 
plot22 <- plot22 + theme_minimal() + ggtitle("Word Count of FIR by Crime Type") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
plot22

#Figure A28
ggarrange(plot2, plot22, ncol=2, nrow=1, labels = c("A)", "B)"))


##
plot3 <- ggplot(data3, aes(x=complainant_gender, y=diff_in_days)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 25)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                position=position_jitter(width=0.1, height=0.2)) 
plot3 <- plot3 + xlab("Complainant Gender") + ylab("Days") 
plot3 <- plot3 + theme_minimal() + ggtitle("Registration Duration") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
plot3


##
plot4 <- ggplot(data3, aes(x=complainant_gender, y=diff_in_days2)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 25)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                position=position_jitter(width=0.1, height=0.2)) 
plot4 <- plot4 + xlab("Complainant Gender") + ylab("Days") 
plot4 <- plot4 + theme_minimal() + ggtitle("Pre-Registration Duration") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
plot4

##
plot5 <- ggplot(data3, aes(x=gendered, y=diff_in_days)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 25)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                position=position_jitter(width=0.1, height=0.2)) 
plot5 <- plot5 + xlab("Crime Type") + ylab("Days") 
plot5 <- plot5 + theme_minimal() + ggtitle("Registration Duration") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
plot5

#
plot6 <- ggplot(data3, aes(x=gendered, y=diff_in_days2)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 25)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                position=position_jitter(width=0.1, height=0.2)) 
plot6 <- plot6 + xlab("Crime Type") + ylab("Days") 
plot6 <- plot6 + theme_minimal() + ggtitle("Pre-Registration Duration") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
plot6

##Figure A3
ggarrange(plot3, plot4, ncol=2, nrow=1, labels = c("A)", "B)"))
##Figure A4
ggarrange(plot5, plot6, ncol=2, nrow=1, labels = c("A)", "B)"))


##
dat.m <- select(data3, diff_in_days, dowry, rape, fem_kidnapping, criminal_force)
dat.m <- melt(dat.m, measure.vars=2:5)

dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)
tapply(dat.m$diff_in_days, dat.m$variable, summary)


plot7 <- ggplot(dat.m, aes(x=variable, y=diff_in_days)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 25)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                position=position_jitter(width=0.1, height=0.2)) 
plot7 <- plot7 + xlab("Crime Type") + ylab("Days") 
plot7 <- plot7 + theme_minimal() + ggtitle("Registration Duration") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
plot7 <- plot7 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=12, hjust=.5, vjust=.5),axis.text.y = element_text(colour="grey20", size=12))
plot7

##
dat.m <- select(data3, diff_in_days2, dowry, rape, fem_kidnapping, criminal_force)
dat.m <- melt(dat.m, measure.vars=2:5)
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)
tapply(dat.m$diff_in_days2, dat.m$variable, summary)

plot8 <- ggplot(dat.m, aes(x=variable, y=diff_in_days2)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 2000)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                position=position_jitter(width=0.1, height=0.2)) 
plot8 <- plot8 + xlab("Crime Type") + ylab("Days") 
plot8 <- plot8 + theme_minimal() + ggtitle("Pre-Registration Duration") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
plot8 <- plot8 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=12, hjust=.5, vjust=.5),axis.text.y = element_text(colour="grey20", size=12))
plot8

##Figure A5
ggarrange(plot7, plot8, ncol=2, nrow=1, labels = c("A)", "B)"))


##
names(data3)
#
dat.m <- select(data3,diff_in_days, 30:66)
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)

tapply(dat.m$diff_in_days, dat.m$variable, summary)

#
dat.m$index <- ifelse(dat.m$variable == "ipc_379", "theft (N=89,222)", 
                      ifelse(dat.m$variable == "ipc_279", "rash driving (N=47,921)", 
                             ifelse(dat.m$variable == "ipc_380", "theft/burglary (N=33,758)", 
                                    ifelse(dat.m$variable == "ipc_457", "trespassing (N=21,905)", 
                                           ifelse(dat.m$variable == "ipc_420", "cheating (N=19,957)",
                                                  ifelse(dat.m$variable == "ipc_346", "wrongful confinement/missing person (N=18,149)",
                                                         ifelse(dat.m$variable == "ipc_304a", "culpable homicide (N=17,282)",
                                                                ifelse(dat.m$variable == "excise", "Excise Act/offenses related to liquor (N=54,194)",
                                                                       ifelse(dat.m$variable == "ipc_149", "unlawful assembly (N=16,462)",
                                                                              ifelse(dat.m$variable == "ipc_341", "wrongful restraint (N=13,357)",
                                                                                     ifelse(dat.m$variable == "ipc_148", "rioting (N=13,010)",
                                                                                            ifelse(dat.m$variable == "arms", "Arms Act/offenses related to weapons (N=14,033)",
                                                                                                   ifelse(dat.m$variable == "ipc_452", "trespassing at night (N=10,590)",
                                                                                                          ifelse(dat.m$variable == "ipc_365", "kidnapping and abducting (N=7,334)",
                                                                                                                 ifelse(dat.m$variable == "ipc_324", "causing hurt with weapons (N=6,805)",
                                                                                                                        ifelse(dat.m$variable == "electricity", "Electricity Act/offenses related to electricity theft (N=7,142)",
                                                                                                                               ifelse(dat.m$variable == "gambling", "Gambling/offenses related to gambling (N=16,039)",
                                                                                                                                      ifelse(dat.m$variable == "ipc_353", "assault on public servant (N=3,862)",
                                                                                                                                             ifelse(dat.m$variable == "ipc_363", "kidnapping (N=3,634)",
                                                                                                                                                    ifelse(dat.m$variable == "ipc_307", "attempt to murder (N=3,206)",
                                                                                                                                                           ifelse(dat.m$variable == "ipc_302", "murder (N=2,774)",
                                                                                                                                                                  ifelse(dat.m$variable == "narcotic", "Narcotics Act/offenses related to narcotics (N=7,562)",
                                                                                                                                                                         ifelse(dat.m$variable == "ipc_366a", "procuration of minor girl (N=2,042)",
                                                                                                                                                                                ifelse(dat.m$variable == "ipc_294", "obscene acts/songs (N=1,595)",
                                                                                                                                                                                       ifelse(dat.m$variable == "ipc_342", "wrongful confinement (N=1,995)",
                                                                                                                                                                                              ifelse(dat.m$variable == "ipc_392", "robbery (N=1,796)",
                                                                                                                                                                                                     ifelse(dat.m$variable == "ipc_306", "abetment of suicide (N=1,466)",
                                                                                                                                                                                                            ifelse(dat.m$variable == "ipc_510", "public misconduct by drunkard (N=1,335)",
                                                                                                                                                                                                                   ifelse(dat.m$variable == "public_property", "Damage to Public Property Act (N=4,743)",
                                                                                                                                                                                                                          ifelse(dat.m$variable == "animals", "Cruelty to Animals Act/offenses related to cattle/cow slaughter (N=3,880)",
                                                                                                                                                                                                                                 ifelse(dat.m$variable == "ipc_509", "word, gesture or act intended to insult modesty of a woman (N=821)",
                                                                                                                                                                                                                                        ifelse(dat.m$variable == "development", "Regulation of Urban Areas Act (N=1,127)",
                                                                                                                                                                                                                                               ifelse(dat.m$variable == "ipc_304b", "dowry death (N=637)",
                                                                                                                                                                                                                                                      ifelse(dat.m$variable == "ipc_354d", "stalking (N=616)",
                                                                                                                                                                                                                                                             ifelse(dat.m$variable == "ipc_354a", "sexual harassment (N=1,195)",
                                                                                                                                                                                                                                                                    ifelse(dat.m$variable == "scst", "Prevention of Atrocities Act/hate crime (N=1,245)","Protection of Children from Sexual Offenses Act (N=1,184)"))))))))))))))))))))))))))))))))))))
#
table(dat.m$variable)
table(dat.m$index)

##
various1 <- ggplot(dat.m, aes(x=reorder(variable,diff_in_days,na.rm = TRUE), y=diff_in_days)) + geom_boxplot(outlier.colour = NA, notch=FALSE) + 
  stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(.5)), aes(label=sprintf("%1.1f", ..y..)),position=position_nudge(x=0), size=3, hjust=1.2) + 
  stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0), size=3, vjust=0.4, hjust=-1, aes(label=round(..y.., digits=1))) + 
  theme(legend.position = "none") + coord_cartesian(ylim = c(0, 100)) + xlab("Indian Penal Code/Special Act") + ylab("Days") + theme_minimal() + ggtitle("Registration Duration") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
various1

dat_include <-dat.m %>% dplyr::group_by(variable) %>% dplyr::summarise(index = max(index))

various1 <- various1 + geom_text(data = dat_include,aes(x= variable, y = 50, label= index), hjust=0, vjust=-0.1) 
various1 <- various1 + coord_flip(ylim = c(0,100), xlim = c(0, 37))
#Figure A6
various1


##
dat.m <- select(data3,diff_in_days2, 30:66)
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)

tapply(dat.m$diff_in_days2, dat.m$variable, summary)


#
dat.m$index <- ifelse(dat.m$variable == "ipc_379", "theft (N=89,222)", 
                      ifelse(dat.m$variable == "ipc_279", "rash driving (N=47,921)", 
                             ifelse(dat.m$variable == "ipc_380", "theft/burglary (N=33,758)", 
                                    ifelse(dat.m$variable == "ipc_457", "trespassing (N=21,905)", 
                                           ifelse(dat.m$variable == "ipc_420", "cheating (N=19,957)",
                                                  ifelse(dat.m$variable == "ipc_346", "wrongful confinement/missing person (N=18,149)",
                                                         ifelse(dat.m$variable == "ipc_304a", "culpable homicide (N=17,282)",
                                                                ifelse(dat.m$variable == "excise", "Excise Act/offenses related to liquor (N=54,194)",
                                                                       ifelse(dat.m$variable == "ipc_149", "unlawful assembly (N=16,462)",
                                                                              ifelse(dat.m$variable == "ipc_341", "wrongful restraint (N=13,357)",
                                                                                     ifelse(dat.m$variable == "ipc_148", "rioting (N=13,010)",
                                                                                            ifelse(dat.m$variable == "arms", "Arms Act/offenses related to weapons (N=14,033)",
                                                                                                   ifelse(dat.m$variable == "ipc_452", "trespassing at night (N=10,590)",
                                                                                                          ifelse(dat.m$variable == "ipc_365", "kidnapping and abducting (N=7,334)",
                                                                                                                 ifelse(dat.m$variable == "ipc_324", "causing hurt with weapons (N=6,805)",
                                                                                                                        ifelse(dat.m$variable == "electricity", "Electricity Act/offenses related to electricity theft (N=7,142)",
                                                                                                                               ifelse(dat.m$variable == "gambling", "Gambling/offenses related to gambling (N=16,039)",
                                                                                                                                      ifelse(dat.m$variable == "ipc_353", "assault on public servant (N=3,862)",
                                                                                                                                             ifelse(dat.m$variable == "ipc_363", "kidnapping (N=3,634)",
                                                                                                                                                    ifelse(dat.m$variable == "ipc_307", "attempt to murder (N=3,206)",
                                                                                                                                                           ifelse(dat.m$variable == "ipc_302", "murder (N=2,774)",
                                                                                                                                                                  ifelse(dat.m$variable == "narcotic", "Narcotics Act/offenses related to narcotics (N=7,562)",
                                                                                                                                                                         ifelse(dat.m$variable == "ipc_366a", "procuration of minor girl (N=2,042)",
                                                                                                                                                                                ifelse(dat.m$variable == "ipc_294", "obscene acts/songs (N=1,595)",
                                                                                                                                                                                       ifelse(dat.m$variable == "ipc_342", "wrongful confinement (N=1,995)",
                                                                                                                                                                                              ifelse(dat.m$variable == "ipc_392", "robbery (N=1,796)",
                                                                                                                                                                                                     ifelse(dat.m$variable == "ipc_306", "abetment of suicide (N=1,466)",
                                                                                                                                                                                                            ifelse(dat.m$variable == "ipc_510", "public misconduct by drunkard (N=1,335)",
                                                                                                                                                                                                                   ifelse(dat.m$variable == "public_property", "Damage to Public Property Act (N=4,743)",
                                                                                                                                                                                                                          ifelse(dat.m$variable == "animals", "Cruelty to Animals Act/offenses related to cattle/cow slaughter (N=3,880)",
                                                                                                                                                                                                                                 ifelse(dat.m$variable == "ipc_509", "word, gesture or act intended to insult modesty of a woman (N=821)",
                                                                                                                                                                                                                                        ifelse(dat.m$variable == "development", "Regulation of Urban Areas Act (N=1,127)",
                                                                                                                                                                                                                                               ifelse(dat.m$variable == "ipc_304b", "dowry death (N=637)",
                                                                                                                                                                                                                                                      ifelse(dat.m$variable == "ipc_354d", "stalking (N=616)",
                                                                                                                                                                                                                                                             ifelse(dat.m$variable == "ipc_354a", "sexual harassment (N=1,195)",
                                                                                                                                                                                                                                                                    ifelse(dat.m$variable == "scst", "Prevention of Atrocities Act/hate crime (N=1,245)","Protection of Children from Sexual Offenses Act (N=1,184)"))))))))))))))))))))))))))))))))))))
#
table(dat.m$variable)
table(dat.m$index)

##
various2 <- ggplot(dat.m, aes(x=reorder(variable,diff_in_days2,na.rm = TRUE), y=diff_in_days2)) + geom_boxplot(outlier.colour = NA, notch=FALSE) + 
  stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(.5)), aes(label=sprintf("%1.1f", ..y..)),position=position_nudge(x=0), size=3, hjust=1.2) + 
  stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0), size=3, vjust=0.4, hjust=-1, aes(label=round(..y.., digits=1))) + 
  theme(legend.position = "none") + coord_cartesian(ylim = c(0, 150)) + xlab("Indian Penal Code/Special Act") + ylab("Days") + theme_minimal() + ggtitle("Pre-Registration Duration") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
various2

dat_include <-dat.m %>% dplyr::group_by(variable) %>% dplyr::summarise(index = max(index))

various2 <- various2 + geom_text(data = dat_include,aes(x= variable, y = 75, label= index), hjust=0, vjust=-0.1) 
various2 <- various2 + coord_flip(ylim = c(0,150), xlim = c(0, 37))
#Figure A7
various2

##
reg0 <- lm(diff_in_days~complainant_fem, data = data3)
vcov_district <- cluster.vcov(reg0, data3$district)
reg0 <- coeftest(reg0, vcov_district)

reg3 <- lm(diff_in_days~complainant_fem  + as.factor(my_station) + Month_Yr + io_rank + distance , data = data3)
vcov_district <- cluster.vcov(reg3, data3$district)
reg3 <- coeftest(reg3, vcov_district)


##
reg00 <- lm(diff_in_days~gendered, data = data3)
vcov_district <- cluster.vcov(reg00, data3$district)
reg00 <- coeftest(reg00, vcov_district)
reg00

reg15 <- lm(diff_in_days~gendered  + as.factor(my_station) + Month_Yr + io_rank + distance , data = data3)
vcov_district <- cluster.vcov(reg15, data3$district)
reg15 <- coeftest(reg15, vcov_district)
reg15


##
reg000 <- lm(diff_in_days~complainant_fem*gendered, data = data3)
vcov_district <- cluster.vcov(reg000, data3$district)
reg000 <- coeftest(reg000, vcov_district)
reg000

reg27 <- lm(diff_in_days~complainant_fem*gendered  + as.factor(my_station) + Month_Yr + io_rank + distance , data = data3)
vcov_district <- cluster.vcov(reg27, data3$district)
reg27 <- coeftest(reg27, vcov_district)
reg27

##Difference in Days (Table 3, Columns 1-6)
stargazer(reg0, reg3, reg00, reg15, reg000, reg27, align = FALSE, type="text",omit.stat=c("ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01", "ps_category","distance"))
stargazer(reg0, reg3, reg00, reg15, reg000, reg27, align = FALSE,omit.stat=c("ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01", "ps_category","distance"))
#stargazer(reg0, reg3, reg00, reg15, reg000, reg27, align = FALSE, omit.stat=c("ser","f"), out="filename.tex")

#
reg27 <- lm(diff_in_days~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance, data = data3)
int0 <- ggeffect(reg27, c("gendered", "complainant_fem"))
int0
int0 <- ggplot(int0, aes(x=x, y=predicted, colour = group, group=group)) +
  geom_point() + geom_line() + geom_errorbar(aes(ymin = conf.low, ymax = conf.high, width=0.05)) +
  scale_x_discrete(breaks = 0:1, expand = expansion(mult = c(0.2, 0.2)), labels=c("0" = "Non-VAW", "1" = "VAW")) + scale_color_manual(labels = c("Male", "Female"), values = c("blue", "red")) +
  theme(axis.ticks=element_blank(), axis.title.x=element_blank(), legend.title=element_blank()) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "grey20")) +
  theme(text = element_text(size = 20)) +theme(legend.position="bottom") +
  geom_text(aes(label = round(predicted,2)),vjust=-0.2, hjust=1.3) +
  ggtitle("Registration Duration") + ylab("Days") + theme(plot.title = element_text(size = 20, hjust=0.5)) 
int0

#
org1 <- lm(diff_in_days~dowry  + rape + fem_kidnapping + criminal_force, data = data3)
vcov_district <- cluster.vcov(org1, data3$district)
org1 <- coeftest(org1, vcov_district)

org2 <- lm(diff_in_days~dowry  + rape + fem_kidnapping + criminal_force + my_station + Month_Yr + io_rank + distance , data = data3)
vcov_district <- cluster.vcov(org2, data3$district)
org2 <- coeftest(org2, vcov_district)

org3 <- lm(diff_in_days~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force, data = data3)
vcov_district <- cluster.vcov(org3, data3$district)
org3 <- coeftest(org3, vcov_district)

org4 <- lm(diff_in_days~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force + my_station + Month_Yr + io_rank + distance , data = data3)
vcov_district <- cluster.vcov(org4, data3$district)
org4 <- coeftest(org4, vcov_district)

#Table A3
stargazer(org1, org2, org3, org4, align = FALSE, type="text", omit.stat=c("ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance"))
stargazer(org1, org2, org3, org4, align = FALSE, omit.stat=c("ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance"))
#stargazer(org1, org2, org3, org4, align = FALSE, omit.stat=c("ser","f"), out="filename.tex")


########################################################################################################################################
#Final2
final2 <- final[!is.na(final$state), ]
unique(final2$ident)#251k/418K have a record in the court
  
#A) All police reports with the latest court entry 
last <- final %>% group_by(ident) %>% slice(match(max(date_last_list), date_last_list)) 

#B) All police reports that have a record in the court, latest entry [251k cases]
last2 <- final2 %>% group_by(ident) %>% slice(match(max(date_last_list), date_last_list)) 

#C) All police reports for which a decision was ultimately made [155k cases]
last3 <- last2[!is.na(last2$my_court_status), ]

#D) All police reports based on the first entry [251k cases]
first <- final2 %>% group_by(ident) %>% slice(match(min(date_of_filing), date_of_filing))

#E) All police reports, NA listed as on-going 
table(last$state,useNA = "always")
last4 <- last
last4$state <- ifelse(is.na(last4$state), "no_record", last4$state)
last4$my_court_status <- ifelse(grepl("no_record", last4$state), "no_record", last4$my_court_status)
last4$my_court_status2 <- ifelse(grepl("no_record", last4$state), "no_record", last4$my_court_status2)
table(last4$my_court_status)
table(last4$my_court_status2)

##
prop.table(table(last$complainant_gender, last$my_court_status2,useNA = "always"),1)
prop.table(table(last2$complainant_gender, last2$my_court_status2,useNA = "always"),1)
prop.table(table(last3$complainant_gender, last3$my_court_status2),1)
prop.table(table(last4$complainant_gender, last4$my_court_status2,useNA = "always"),1)

##
last2$my_court_status2 <- ifelse(is.na(last2$my_court_status2), "ongoing", last2$my_court_status2) 
last4$my_court_status2 <- ifelse(is.na(last4$my_court_status2), "ongoing", last4$my_court_status2)

##
last2$my_court_status2 <- factor(last2$my_court_status2,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other"))
last3$my_court_status2 <- factor(last3$my_court_status2,levels = c("convicted", "acquitted", "dismissed_cancelled","untraced_abated","allowed","disposed_other"))
last4$my_court_status2 <- factor(last4$my_court_status2,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))

##
complainant1 <- last4 %>% freq_table(complainant_gender, my_court_status2)
complainant1$col_cat <- factor(complainant1$col_cat,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))


fig1 <- ggplot(complainant1, aes(col_cat, percent_row, group=row_cat, ymin=lcl_row, ymax=ucl_row)) + 
  geom_col(aes(fill = row_cat),position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent_row,1)), vjust=-1.5, position = position_dodge(0.5))
fig1 <- fig1 + theme(axis.ticks=element_blank(), axis.text.y = element_blank(), axis.title.x=element_blank(), legend.title=element_blank(), axis.title.y=element_blank()) + theme(text = element_text(size = 20)) 
fig1 <- fig1 + theme(plot.title = element_text(size = 15, hjust=0.5)) + ggtitle("Court Status of Crime Reports by Complainant Gender") 
fig1 <- fig1 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=13, hjust=.5, vjust=.5),
                     axis.text.y = element_text(colour="grey20", size=12)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                   panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 50, 5), limits=c(0, 50)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl_row, ymax = ucl_row),position = position_dodge(0.5), width = 0.4)
fig1 <- fig1 + scale_fill_manual(values = c("gray71", "grey40"), labels=c("Female","Other")) + theme(legend.position="bottom")
fig1 

##
complainant2 <- last4 %>% freq_table(gendered, my_court_status2)
complainant2$col_cat <- factor(complainant2$col_cat,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))

fig2 <- ggplot(complainant2, aes(col_cat, percent_row, group=row_cat, ymin=lcl_row, ymax=ucl_row)) + 
  geom_col(aes(fill = row_cat),position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent_row,1)), vjust=-1.5, position = position_dodge(0.5))
fig2 <- fig2 + theme(axis.ticks=element_blank(), axis.text.y = element_blank(), axis.title.x=element_blank(), legend.title=element_blank(), axis.title.y=element_blank()) + theme(text = element_text(size = 20)) 
fig2 <- fig2 + theme(plot.title = element_text(size = 15, hjust=0.5)) + ggtitle("Court Status of Crime Reports by Crime Type") 
fig2 <- fig2 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=13, hjust=.5, vjust=.5),
                     axis.text.y = element_text(colour="grey20", size=12)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                   panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 50, 5), limits=c(0, 50)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl_row, ymax = ucl_row),position = position_dodge(0.5), width = 0.4)
fig2 <- fig2 + scale_fill_manual(values = c("gray71", "grey40"), labels=c("Non-VAW","VAW")) + theme(legend.position="bottom")
fig2 

ggarrange(fig1, fig2, labels = c("A)", "B)"), legend="bottom")


##
dowry <- last4[with(last4, grepl("1", last4$dowry)),]

preference1 <- dowry %>% freq_table(my_court_status2)
prop.table(table(dowry$my_court_status2))
preference1$cat <- factor(preference1$cat,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))

fig3 <- ggplot(preference1, aes(cat, percent, ymin=lcl, ymax=ucl)) + 
  geom_col(position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent,1)), vjust=-2, position = position_dodge(0.5))
fig3 <- fig3 + theme(axis.ticks=element_blank(), axis.text.y = element_blank(), axis.title.x=element_blank(), legend.title=element_blank(), axis.title.y=element_blank()) + theme(text = element_text(size = 20)) 
fig3 <- fig3 + theme(plot.title = element_text(size = 15, hjust=0.5)) + ggtitle("Status of Crime Reports [Dowry]") 
fig3 <- fig3 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=13, hjust=.5, vjust=.5),
                     axis.text.y = element_text(colour="grey20", size=12)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                   panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 60, 5), limits=c(0, 60)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl, ymax = ucl),position = position_dodge(0.5), width = 0.4)
fig3 


#
rape <- last4[with(last4, grepl("1", last4$rape)),]

prop.table(table(rape$my_court_status2))
preference2 <- rape %>% freq_table(my_court_status2)
preference2$cat <- factor(preference2$cat,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))


fig4 <- ggplot(preference2, aes(cat, percent, ymin=lcl, ymax=ucl)) + 
  geom_col(position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent,1)), vjust=-4, position = position_dodge(0.5))
fig4 <- fig4 + theme(axis.ticks=element_blank(), axis.text.y = element_blank(), axis.title.x=element_blank(), legend.title=element_blank(), axis.title.y=element_blank()) + theme(text = element_text(size = 20)) 
fig4 <- fig4 + theme(plot.title = element_text(size = 15, hjust=0.5)) + ggtitle("Status of Crime Reports [Rape]") 
fig4 <- fig4 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=13, hjust=.5, vjust=.5),
                     axis.text.y = element_text(colour="grey20", size=12)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                   panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 60, 5), limits=c(0, 60)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl, ymax = ucl),position = position_dodge(0.5), width = 0.4)
fig4

##
fem_kidnapping <- last4[with(last4, grepl("1", last4$fem_kidnapping)),]
prop.table(table(fem_kidnapping$my_court_status2))
preference3 <- fem_kidnapping %>% freq_table(my_court_status2)
preference3$cat <- factor(preference3$cat,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))


fig5 <- ggplot(preference3, aes(cat, percent, ymin=lcl, ymax=ucl)) + 
  geom_col(position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent,1)), vjust=-2, position = position_dodge(0.5))
fig5 <- fig5 + theme(axis.ticks=element_blank(), axis.text.y = element_blank(), axis.title.x=element_blank(), legend.title=element_blank(), axis.title.y=element_blank()) + theme(text = element_text(size = 20)) 
fig5 <- fig5 + theme(plot.title = element_text(size = 15, hjust=0.5)) + ggtitle("Status of Crime Reports [Female Kidnapping]") 
fig5 <- fig5 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=13, hjust=.5, vjust=.5),
                     axis.text.y = element_text(colour="grey20", size=12)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                   panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 60, 5), limits=c(0, 60)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl, ymax = ucl),position = position_dodge(0.5), width = 0.4)
fig5

##
criminal_force <- last4[with(last4, grepl("1", last4$criminal_force)),]
prop.table(table(criminal_force$my_court_status2))
preference4 <- criminal_force %>% freq_table(my_court_status2)
preference4$cat <- factor(preference4$cat,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))


fig6 <- ggplot(preference4, aes(cat, percent, ymin=lcl, ymax=ucl)) + 
  geom_col(position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent,1)), vjust=-2, position = position_dodge(0.5))
fig6 <- fig6 + theme(axis.ticks=element_blank(), axis.text.y = element_blank(), axis.title.x=element_blank(), legend.title=element_blank(), axis.title.y=element_blank()) + theme(text = element_text(size = 20)) 
fig6 <- fig6 + theme(plot.title = element_text(size = 15, hjust=0.5)) + ggtitle("Status of Crime Reports [Criminal Force]") 
fig6 <- fig6 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=13, hjust=.5, vjust=.5),
                     axis.text.y = element_text(colour="grey20", size=12)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                   panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 60, 5), limits=c(0, 60)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl, ymax = ucl),position = position_dodge(0.5), width = 0.4)
fig6

#Figure A23
ggarrange(fig3, fig4, fig5, fig6, labels = c("A)", "B)", "C)", "D)"), legend="bottom")


####################################################################################################################
complainant1 <- last2 %>% freq_table(complainant_gender, my_court_status2)
complainant1$col_cat <- factor(complainant1$col_cat,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))


fig7 <- ggplot(complainant1, aes(col_cat, percent_row, group=row_cat, ymin=lcl_row, ymax=ucl_row)) + 
  geom_col(aes(fill = row_cat),position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent_row,1)), vjust=-1.5, position = position_dodge(0.5))
fig7 <- fig7 + theme(axis.ticks=element_blank(), axis.text.y = element_blank(), axis.title.x=element_blank(), legend.title=element_blank(), axis.title.y=element_blank()) + theme(text = element_text(size = 20)) 
fig7 <- fig7 + theme(plot.title = element_text(size = 15, hjust=0.5)) + ggtitle("Court Status of Crime Reports by Complainant Gender [Court Docket]") 
fig7 <- fig7 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=13, hjust=.5, vjust=.5),
                     axis.text.y = element_text(colour="grey20", size=12)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                   panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 50, 5), limits=c(0, 50)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl_row, ymax = ucl_row),position = position_dodge(0.5), width = 0.4)
fig7 <- fig7 + scale_fill_manual(values = c("gray71", "grey40"), labels=c("Female","Other")) + theme(legend.position="bottom")
fig7 


##
complainant2 <- last2 %>% freq_table(gendered, my_court_status2)
complainant2$col_cat <- factor(complainant2$col_cat,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))

fig8 <- ggplot(complainant2, aes(col_cat, percent_row, group=row_cat, ymin=lcl_row, ymax=ucl_row)) + 
  geom_col(aes(fill = row_cat),position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent_row,1)), vjust=-1.5, position = position_dodge(0.5))
fig8 <- fig8 + theme(axis.ticks=element_blank(), axis.text.y = element_blank(), axis.title.x=element_blank(), legend.title=element_blank(), axis.title.y=element_blank()) + theme(text = element_text(size = 20)) 
fig8 <- fig8 + theme(plot.title = element_text(size = 15, hjust=0.5)) + ggtitle("Court Status of Crime Reports by Crime Type [Court Docket]") 
fig8 <- fig8 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=13, hjust=.5, vjust=.5),
                     axis.text.y = element_text(colour="grey20", size=12)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                   panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 50, 5), limits=c(0, 50)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl_row, ymax = ucl_row),position = position_dodge(0.5), width = 0.4)
fig8 <- fig8 + scale_fill_manual(values = c("gray71", "grey40"), labels=c("Non-VAW","VAW")) + theme(legend.position="bottom")
fig8 

ggarrange(fig7, fig8, labels = c("A)", "B)"), legend="bottom")

##
fig1.1 <- fig1 + theme(legend.position = "none") 
fig2.1 <- fig2 + theme(legend.position = "none") 
fig1.1
fig2.1

#Figure 3
ggarrange(fig1.1, fig2.1, fig7, fig8, labels = c("A)", "B)", "C)", "D)"))


##
dowry <- last2[with(last2, grepl("1", last2$dowry)),]

preference1 <- dowry %>% freq_table(my_court_status2)
prop.table(table(dowry$my_court_status2))
preference1$cat <- factor(preference1$cat,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))

fig9 <- ggplot(preference1, aes(cat, percent, ymin=lcl, ymax=ucl)) + 
  geom_col(position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent,1)), vjust=-2, position = position_dodge(0.5))
fig9 <- fig9 + theme(axis.ticks=element_blank(), axis.text.y = element_blank(), axis.title.x=element_blank(), legend.title=element_blank(), axis.title.y=element_blank()) + theme(text = element_text(size = 20)) 
fig9 <- fig9 + theme(plot.title = element_text(size = 15, hjust=0.5)) + ggtitle("Status of Crime Reports [Dowry, Court Docket]") 
fig9 <- fig9 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=12, hjust=.5, vjust=.5),
                     axis.text.y = element_text(colour="grey20", size=12)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                   panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 60, 5), limits=c(0, 60)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl, ymax = ucl),position = position_dodge(0.5), width = 0.4)
fig9 


#rape
rape <- last2[with(last2, grepl("1", last2$rape)),]

prop.table(table(rape$my_court_status2))
preference2 <- rape %>% freq_table(my_court_status2)
preference2$cat <- factor(preference2$cat,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))


fig10 <- ggplot(preference2, aes(cat, percent, ymin=lcl, ymax=ucl)) + 
  geom_col(position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent,1)), vjust=-4, position = position_dodge(0.5))
fig10 <- fig10 + theme(axis.ticks=element_blank(), axis.text.y = element_blank(), axis.title.x=element_blank(), legend.title=element_blank(), axis.title.y=element_blank()) + theme(text = element_text(size = 20)) 
fig10 <- fig10 + theme(plot.title = element_text(size = 15, hjust=0.5)) + ggtitle("Status of Crime Reports [Rape, Court Docket]") 
fig10 <- fig10 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=12, hjust=.5, vjust=.5),
                       axis.text.y = element_text(colour="grey20", size=12)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                     panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 60, 5), limits=c(0, 60)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl, ymax = ucl),position = position_dodge(0.5), width = 0.4)
fig10

##
fem_kidnapping <- last2[with(last2, grepl("1", last2$fem_kidnapping)),]

prop.table(table(fem_kidnapping$my_court_status2))
preference3 <- fem_kidnapping %>% freq_table(my_court_status2)
preference3$cat <- factor(preference3$cat,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))


fig11 <- ggplot(preference3, aes(cat, percent, ymin=lcl, ymax=ucl)) + 
  geom_col(position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent,1)), vjust=-2, position = position_dodge(0.5))
fig11 <- fig11 + theme(axis.ticks=element_blank(), axis.text.y = element_blank(), axis.title.x=element_blank(), legend.title=element_blank(), axis.title.y=element_blank()) + theme(text = element_text(size = 20)) 
fig11 <- fig11 + theme(plot.title = element_text(size = 15, hjust=0.5)) + ggtitle("Status of Crime Reports [Female Kidnapping, Court Docket]") 
fig11 <- fig11 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=12, hjust=.5, vjust=.5),
                       axis.text.y = element_text(colour="grey20", size=12)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                     panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 60, 5), limits=c(0, 60)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl, ymax = ucl),position = position_dodge(0.5), width = 0.4)
fig11

##
criminal_force <- last2[with(last2, grepl("1", last2$criminal_force)),]

prop.table(table(criminal_force$my_court_status2))
preference4 <- criminal_force %>% freq_table(my_court_status2)
preference4$cat <- factor(preference4$cat,levels = c("convicted", "acquitted", "dismissed_cancelled","ongoing","untraced_abated","allowed","disposed_other", "no_record"))


fig12 <- ggplot(preference4, aes(cat, percent, ymin=lcl, ymax=ucl)) + 
  geom_col(position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent,1)), vjust=-2, position = position_dodge(0.5))
fig12 <- fig12 + theme(axis.ticks=element_blank(), axis.text.y = element_blank(), axis.title.x=element_blank(), legend.title=element_blank(), axis.title.y=element_blank()) + theme(text = element_text(size = 20)) 
fig12 <- fig12 + theme(plot.title = element_text(size = 15, hjust=0.5)) + ggtitle("Status of Crime Reports [Criminal Force, Court Docket]") 
fig12 <- fig12 + theme(axis.text.x = element_text(angle = 30, colour="grey20", size=12, hjust=.5, vjust=.5),
                       axis.text.y = element_text(colour="grey20", size=12)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                     panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 60, 5), limits=c(0, 60)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl, ymax = ucl),position = position_dodge(0.5), width = 0.4)
fig12

##Figure A18
ggarrange(fig9, fig10, fig11, fig12, labels = c("A)", "B)", "C)", "D)"), legend="bottom")


######################################################
last2$convicted <- ifelse(grepl("convicted", last2$my_court_status2), 1, 0)
last2$acquitted <- ifelse(grepl("acquitted", last2$my_court_status2), 1, 0)

#
names(last2)
dat.m <- select(last2,convicted, 30:66)
dat.m$ident <- NULL 
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)

##
prop.table(table(dat.m$variable, dat.m$convicted),1)

#
dat.m$index <- ifelse(dat.m$variable == "ipc_379", "theft (N=38,888)", 
                      ifelse(dat.m$variable == "ipc_279", "rash driving (N=35,214)", 
                             ifelse(dat.m$variable == "ipc_380", "theft/burglary (N=13,578)", 
                                    ifelse(dat.m$variable == "ipc_457", "trespassing (N=8,906)", 
                                           ifelse(dat.m$variable == "ipc_420", "cheating (N=10,346)",
                                                  ifelse(dat.m$variable == "ipc_346", "wrongful confinement/missing person (N=4,364)",
                                                         ifelse(dat.m$variable == "ipc_304a", "culpable homicide (N=12,053)",
                                                                ifelse(dat.m$variable == "excise", "Excise Act/offenses related to liquor (N=37,679)",
                                                                       ifelse(dat.m$variable == "ipc_149", "unlawful assembly (N=12,426)",
                                                                              ifelse(dat.m$variable == "ipc_341", "wrongful restraint (N=10,446)",
                                                                                     ifelse(dat.m$variable == "ipc_148", "rioting (N=10,007)",
                                                                                            ifelse(dat.m$variable == "arms", "Arms Act/offenses related to weapons (N=11,493)",
                                                                                                   ifelse(dat.m$variable == "ipc_452", "trespassing at night (N=8,368)",
                                                                                                          ifelse(dat.m$variable == "ipc_365", "kidnapping and abducting (N=2,322)",
                                                                                                                 ifelse(dat.m$variable == "ipc_324", "causing hurt with weapons (N=5,440)",
                                                                                                                        ifelse(dat.m$variable == "electricity", "Electricity Act/offenses related to electricity theft (N=2,470)",
                                                                                                                               ifelse(dat.m$variable == "gambling", "Gambling/offenses related to gambling (N=13,795)",
                                                                                                                                      ifelse(dat.m$variable == "ipc_353", "assault on public servant (N=2,932)",
                                                                                                                                             ifelse(dat.m$variable == "ipc_363", "kidnapping (N=1,654)",
                                                                                                                                                    ifelse(dat.m$variable == "ipc_307", "attempt to murder (N=2,629)",
                                                                                                                                                           ifelse(dat.m$variable == "ipc_302", "murder (N=1,922)",
                                                                                                                                                                  ifelse(dat.m$variable == "narcotic", "Narcotics Act/offenses related to narcotics (N=7,022)",
                                                                                                                                                                         ifelse(dat.m$variable == "ipc_366a", "procuration of minor girl (N=1,143)",
                                                                                                                                                                                ifelse(dat.m$variable == "ipc_294", "obscene acts/songs (N=1,595)",
                                                                                                                                                                                       ifelse(dat.m$variable == "ipc_342", "wrongful confinement (N=1,350)",
                                                                                                                                                                                              ifelse(dat.m$variable == "ipc_392", "robbery (N=1,107)",
                                                                                                                                                                                                     ifelse(dat.m$variable == "ipc_306", "abetment of suicide (N=844)",
                                                                                                                                                                                                            ifelse(dat.m$variable == "ipc_510", "public misconduct by drunkard (N=1,224)",
                                                                                                                                                                                                                   ifelse(dat.m$variable == "public_property", "Damage to Public Property Act (N=3,949)",
                                                                                                                                                                                                                          ifelse(dat.m$variable == "animals", "Cruelty to Animals Act/offenses related to cattle/cow slaughter (N=3,338)",
                                                                                                                                                                                                                                 ifelse(dat.m$variable == "ipc_509", "word, gesture or act intended to insult modesty of a woman (N=559)",
                                                                                                                                                                                                                                        ifelse(dat.m$variable == "development", "Regulation of Urban Areas Act (N=586)",
                                                                                                                                                                                                                                               ifelse(dat.m$variable == "ipc_304b", "dowry death (N=542)",
                                                                                                                                                                                                                                                      ifelse(dat.m$variable == "ipc_354d", "stalking (N=443)",
                                                                                                                                                                                                                                                             ifelse(dat.m$variable == "ipc_354a", "sexual harassment (N=834)",
                                                                                                                                                                                                                                                                    ifelse(dat.m$variable == "scst", "Prevention of Atrocities Act/hate crime (N=814)","Protection of Children from Sexual Offenses Act (N=993)"))))))))))))))))))))))))))))))))))))
#
table(dat.m$variable)
table(dat.m$index)

##
outcome1 <- dat.m %>% freq_table(variable, convicted)
outcome1 <- outcome1[with(outcome1, grepl(1, outcome1$col_cat)),]

last2_include <-dat.m %>% dplyr::group_by(variable) %>% dplyr::summarise(index = max(index))

##
names(last2_include)[1] <- "row_cat"
outcome1 <- left_join(outcome1, last2_include)

fig13 <- ggplot(outcome1, aes(x=reorder(row_cat,percent_row), percent_row, group=row_cat, ymin=lcl_row, ymax=ucl_row)) + 
  geom_col(aes(fill = row_var),position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent_row,1)), vjust=0, hjust=-1, position = position_dodge(0.5)) 
fig13 <- fig13 + xlab("Indian Penal Code/Special Act") + ylab("Percent") + theme(text = element_text(size = 20)) 
fig13 <- fig13 + theme(plot.title = element_text(size = 20, hjust=0.5)) + ggtitle("Conviction Percentages (Court Docket)") 
fig13 <- fig13 + theme(axis.text.x = element_text(colour="grey20", size=15, hjust=.5, vjust=.5),
                       axis.text.y = element_text(colour="grey20", size=15)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                     panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 100, 5), limits=c(0, 150)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl_row, ymax = ucl_row),position = position_dodge(0.5), width = 0.4)
fig13 <- fig13 + coord_flip() + theme(legend.position = "none") + scale_fill_manual(values = c("grey71"), labels=c("variable"))
fig13 <- fig13 + geom_text(data = outcome1,aes(x= row_cat, y = 55, label= index), hjust=0, vjust=-0.1) 

#Figure A20
fig13

###
names(last2)
dat.m <- select(last2,acquitted, 30:66)
dat.m$ident <- NULL 
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)

##
prop.table(table(dat.m$variable, dat.m$acquitted),1)

##
outcome2 <- dat.m %>% freq_table(variable, acquitted)
outcome2 <- outcome2[with(outcome2, grepl(1, outcome2$col_cat)),]

##
names(last2_include)[1] <- "row_cat"
outcome2 <- left_join(outcome2, last2_include)

fig14 <- ggplot(outcome2, aes(x=reorder(row_cat,percent_row), percent_row, group=row_cat, ymin=lcl_row, ymax=ucl_row)) + 
  geom_col(aes(fill = row_var),position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent_row,1)), vjust=0, hjust=-1, position = position_dodge(0.5))
fig14 <- fig14 + xlab("Indian Penal Code/Special Act") + ylab("Percent") + theme(text = element_text(size = 20)) 
fig14 <- fig14 + theme(plot.title = element_text(size = 20, hjust=0.5)) + ggtitle("Acquittal Percentages (Court Docket)") 
fig14 <- fig14 + theme(axis.text.x = element_text(colour="grey20", size=15, hjust=.5, vjust=.5),
                       axis.text.y = element_text(colour="grey20", size=15)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                     panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 100, 5), limits=c(0, 120)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl_row, ymax = ucl_row),position = position_dodge(0.5), width = 0.4)
fig14 <- fig14 + coord_flip() + theme(legend.position = "none") + scale_fill_manual(values = c("grey71"), labels=c("variable"))
fig14 <- fig14 + geom_text(data = outcome2,aes(x= row_cat, y = 55, label= index), hjust=0, vjust=-0.1) 

#Figure A21
fig14


##
last2$dismissed<- ifelse(grepl("dismissed_cancelled", last2$my_court_status2), 1, 0)
names(last2)

dat.m <- select(last2,dismissed, 30:66)
dat.m$ident <- NULL 
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)#

##
prop.table(table(dat.m$variable, dat.m$dismissed),1)

##
outcome2 <- dat.m %>% freq_table(variable, dismissed)
outcome2 <- outcome2[with(outcome2, grepl(1, outcome2$col_cat)),]

##
names(last2_include)[1] <- "row_cat"
outcome2 <- left_join(outcome2, last2_include)

new <- ggplot(outcome2, aes(x=reorder(row_cat,percent_row), percent_row, group=row_cat, ymin=lcl_row, ymax=ucl_row)) + 
  geom_col(aes(fill = row_var),position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent_row,1)), vjust=0, hjust=-1, position = position_dodge(0.5))
new <- new + xlab("Indian Penal Code/Special Act") + ylab("Percent") + theme(text = element_text(size = 20)) 
new <- new + theme(plot.title = element_text(size = 20, hjust=0.5)) + ggtitle("Dismissal Percentages (Court Docket)") 
new <- new + theme(axis.text.x = element_text(colour="grey20", size=15, hjust=.5, vjust=.5),
                       axis.text.y = element_text(colour="grey20", size=15)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                     panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 100, 5), limits=c(0, 120)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl_row, ymax = ucl_row),position = position_dodge(0.5), width = 0.4)
new <- new+ coord_flip() + theme(legend.position = "none") + scale_fill_manual(values = c("grey71"), labels=c("variable"))
new <- new + geom_text(data = outcome2,aes(x= row_cat, y = 55, label= index), hjust=0, vjust=-0.1) 
#Figure A19
new

###################
##All 
last4$convicted <- ifelse(grepl("convicted", last4$my_court_status2), 1, 0)
last4$acquitted <- ifelse(grepl("acquitted", last4$my_court_status2), 1, 0)

names(last4)
#
dat.m <- select(last4,convicted, 30:66)
dat.m$ident <- NULL
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)

##
prop.table(table(dat.m$variable, dat.m$convicted),1)

#
dat.m$index <- ifelse(dat.m$variable == "ipc_379", "theft (N=89,222)", 
                      ifelse(dat.m$variable == "ipc_279", "rash driving (N=47,921)", 
                             ifelse(dat.m$variable == "ipc_380", "theft/burglary (N=33,758)", 
                                    ifelse(dat.m$variable == "ipc_457", "trespassing (N=21,905)", 
                                           ifelse(dat.m$variable == "ipc_420", "cheating (N=19,957)",
                                                  ifelse(dat.m$variable == "ipc_346", "wrongful confinement/missing person (N=18,149)",
                                                         ifelse(dat.m$variable == "ipc_304a", "culpable homicide (N=17,282)",
                                                                ifelse(dat.m$variable == "excise", "Excise Act/offenses related to liquor (N=54,194)",
                                                                       ifelse(dat.m$variable == "ipc_149", "unlawful assembly (N=16,462)",
                                                                              ifelse(dat.m$variable == "ipc_341", "wrongful restraint (N=13,357)",
                                                                                     ifelse(dat.m$variable == "ipc_148", "rioting (N=13,010)",
                                                                                            ifelse(dat.m$variable == "arms", "Arms Act/offenses related to weapons (N=14,033)",
                                                                                                   ifelse(dat.m$variable == "ipc_452", "trespassing at night (N=10,590)",
                                                                                                          ifelse(dat.m$variable == "ipc_365", "kidnapping and abducting (N=7,334)",
                                                                                                                 ifelse(dat.m$variable == "ipc_324", "causing hurt with weapons (N=6,805)",
                                                                                                                        ifelse(dat.m$variable == "electricity", "Electricity Act/offenses related to electricity theft (N=7,142)",
                                                                                                                               ifelse(dat.m$variable == "gambling", "Gambling/offenses related to gambling (N=16,039)",
                                                                                                                                      ifelse(dat.m$variable == "ipc_353", "assault on public servant (N=3,862)",
                                                                                                                                             ifelse(dat.m$variable == "ipc_363", "kidnapping (N=3,634)",
                                                                                                                                                    ifelse(dat.m$variable == "ipc_307", "attempt to murder (N=3,206)",
                                                                                                                                                           ifelse(dat.m$variable == "ipc_302", "murder (N=2,774)",
                                                                                                                                                                  ifelse(dat.m$variable == "narcotic", "Narcotics Act/offenses related to narcotics (N=7,562)",
                                                                                                                                                                         ifelse(dat.m$variable == "ipc_366a", "procuration of minor girl (N=2,042)",
                                                                                                                                                                                ifelse(dat.m$variable == "ipc_294", "obscene acts/songs (N=1,595)",
                                                                                                                                                                                       ifelse(dat.m$variable == "ipc_342", "wrongful confinement (N=1,995)",
                                                                                                                                                                                              ifelse(dat.m$variable == "ipc_392", "robbery (N=1,796)",
                                                                                                                                                                                                     ifelse(dat.m$variable == "ipc_306", "abetment of suicide (N=1,466)",
                                                                                                                                                                                                            ifelse(dat.m$variable == "ipc_510", "public misconduct by drunkard (N=1,335)",
                                                                                                                                                                                                                   ifelse(dat.m$variable == "public_property", "Damage to Public Property Act (N=4,743)",
                                                                                                                                                                                                                          ifelse(dat.m$variable == "animals", "Cruelty to Animals Act/offenses related to cattle/cow slaughter (N=3,880)",
                                                                                                                                                                                                                                 ifelse(dat.m$variable == "ipc_509", "word, gesture or act intended to insult modesty of a woman (N=821)",
                                                                                                                                                                                                                                        ifelse(dat.m$variable == "development", "Regulation of Urban Areas Act (N=1,127)",
                                                                                                                                                                                                                                               ifelse(dat.m$variable == "ipc_304b", "dowry death (N=637)",
                                                                                                                                                                                                                                                      ifelse(dat.m$variable == "ipc_354d", "stalking (N=616)",
                                                                                                                                                                                                                                                             ifelse(dat.m$variable == "ipc_354a", "sexual harassment (N=1,195)",
                                                                                                                                                                                                                                                                    ifelse(dat.m$variable == "scst", "Prevention of Atrocities Act/hate crime (N=1,245)","Protection of Children from Sexual Offenses Act (N=1,184)"))))))))))))))))))))))))))))))))))))
#
table(dat.m$variable)
table(dat.m$index)


last4_include <-dat.m %>% dplyr::group_by(variable) %>% dplyr::summarise(index = max(index))

##
outcome1 <- dat.m %>% freq_table(variable, convicted)
outcome1 <- outcome1[with(outcome1, grepl(1, outcome1$col_cat)),]

##
names(last4_include)[1] <- "row_cat"
outcome1 <- left_join(outcome1, last4_include)


fig15 <- ggplot(outcome1, aes(x=reorder(row_cat,percent_row), percent_row, group=row_cat, ymin=lcl_row, ymax=ucl_row)) + 
  geom_col(aes(fill = row_var),position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent_row,1)), vjust=0, hjust=-1, position = position_dodge(0.5)) 
fig15 <- fig15 + xlab("Indian Penal Code/Special Act") + ylab("Percent") + theme(text = element_text(size = 20)) 
fig15 <- fig15 + theme(plot.title = element_text(size = 20, hjust=0.5)) + ggtitle("Conviction Percentages (All FIRs)") 
fig15 <- fig15 + theme(axis.text.x = element_text(colour="grey20", size=15, hjust=.5, vjust=.5),
                       axis.text.y = element_text(colour="grey20", size=15)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                     panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 100, 5), limits=c(0, 150)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl_row, ymax = ucl_row),position = position_dodge(0.5), width = 0.4)
fig15 <- fig15 + coord_flip() + theme(legend.position = "none") + scale_fill_manual(values = c("grey71"), labels=c("variable"))
fig15 <- fig15 + geom_text(data = outcome1,aes(x= row_cat, y = 75, label= index), hjust=0, vjust=-0.1) 

##Figure A25
fig15

##
names(last4)
dat.m <- select(last4,acquitted, 30:66)
dat.m$ident <- NULL 
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)

#
prop.table(table(dat.m$variable, dat.m$acquitted),1)

##
outcome2 <- dat.m %>% freq_table(variable, acquitted)
outcome2 <- outcome2[with(outcome2, grepl(1, outcome2$col_cat)),]

##
names(last4_include)[1] <- "row_cat"
outcome2 <- left_join(outcome2, last4_include)


fig16 <- ggplot(outcome2, aes(x=reorder(row_cat,percent_row), percent_row, group=row_cat, ymin=lcl_row, ymax=ucl_row)) + 
  geom_col(aes(fill = row_var),position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent_row,1)), vjust=0, hjust=-1, position = position_dodge(0.5))
fig16 <- fig16 + xlab("Indian Penal Code/Special Act") + ylab("Percent") + theme(text = element_text(size = 20)) 
fig16 <- fig16 + theme(plot.title = element_text(size = 20, hjust=0.5)) + ggtitle("Acquittal Percentages (All FIRs)") 
fig16 <- fig16 + theme(axis.text.x = element_text(colour="grey20", size=15, hjust=.5, vjust=.5),
                       axis.text.y = element_text(colour="grey20", size=15)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                     panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(0, 100, 5), limits=c(0, 120)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl_row, ymax = ucl_row),position = position_dodge(0.5), width = 0.4)
fig16 <- fig16 + coord_flip() + theme(legend.position = "none") + scale_fill_manual(values = c("grey71"), labels=c("variable"))
fig16 <- fig16 + geom_text(data = outcome2,aes(x= row_cat, y = 55, label= index), hjust=0, vjust=-0.1) 
#Figure A26
fig16

########################################################################################################
##
last2$ongoing <- ifelse(is.na(last2$disp_name), 1, 0) 

names(last2)

dat.m <- select(last2,ongoing, 30:66)
dat.m$ident <- NULL 
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)

##
prop.table(table(dat.m$variable, dat.m$ongoing),1)

##
outcome2 <- dat.m %>% freq_table(variable, ongoing)
outcome2 <- outcome2[with(outcome2, grepl(1, outcome2$col_cat)),]

##
names(last2_include)[1] <- "row_cat"
outcome2 <- left_join(outcome2, last2_include)


fig17 <- ggplot(outcome2, aes(x=reorder(row_cat,percent_row), percent_row, group=row_cat, ymin=lcl_row, ymax=ucl_row)) + 
  geom_col(aes(fill = row_var),position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent_row,1)), vjust=0, hjust=-1, position = position_dodge(0.5))
fig17 <- fig17 + xlab("Indian Penal Code/Special Act") + ylab("Percent") + theme(text = element_text(size = 20)) 
fig17 <- fig17 + theme(plot.title = element_text(size = 20, hjust=0.5)) + ggtitle("Percentages of Cases Ongoing (Court Docket)") 
fig17 <- fig17 + theme(axis.text.x = element_text(colour="grey20", size=15, hjust=5, vjust=.5),
                       axis.text.y = element_text(colour="grey20", size=15)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                     panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(10, 100, 10), limits=c(0, 120)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl_row, ymax = ucl_row),position = position_dodge(0.5), width = 0.4)
fig17 <- fig17 + coord_flip() + theme(legend.position = "none") + scale_fill_manual(values = c("grey71"), labels=c("variable"))
fig17 <- fig17 + geom_text(data = outcome2,aes(x= row_cat, y = 80, label= index), hjust=0, vjust=-0.1) 
#Figure A22
fig17

########################################################################################################
##no record
names(last4)
last4$norecord <- ifelse(grepl("no_record", last4$my_court_status2), 1, 0)

dat.m <- select(last4,norecord, 30:66)
dat.m$ident <- NULL
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)

##
prop.table(table(dat.m$variable, dat.m$norecord),1)

##
outcome2 <- dat.m %>% freq_table(variable, norecord)
outcome2 <- outcome2[with(outcome2, grepl(1, outcome2$col_cat)),]

##
names(last4_include)[1] <- "row_cat"
outcome2 <- left_join(outcome2, last4_include)


fig18 <- ggplot(outcome2, aes(x=reorder(row_cat,percent_row), percent_row, group=row_cat, ymin=lcl_row, ymax=ucl_row)) + 
  geom_col(aes(fill = row_var),position = position_dodge(0.5), width = 0.4) +
  geom_text(aes(label = round(percent_row,1)), vjust=0, hjust=-1, position = position_dodge(0.5))
fig18 <- fig18 + xlab("Indian Penal Code/Special Act") + ylab("Percent") + theme(text = element_text(size = 20)) 
fig18 <- fig18 + theme(plot.title = element_text(size = 20, hjust=0.5)) + ggtitle("Percentages of Cases from All FIRs That Do Not Have a Court Record") 
fig18 <- fig18 + theme(axis.text.x = element_text(colour="grey20", size=15, hjust=5, vjust=.5),
                       axis.text.y = element_text(colour="grey20", size=15)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                                                                     panel.background = element_blank(), axis.line = element_line(colour = "black")) + scale_y_continuous(breaks = seq(10, 100, 10), limits=c(0, 120)) + theme(text = element_text(size = 20)) + geom_errorbar(aes(ymin = lcl_row, ymax = ucl_row),position = position_dodge(0.5), width = 0.4)
fig18 <- fig18 + coord_flip() + theme(legend.position = "none") + scale_fill_manual(values = c("grey71"), labels=c("variable"))
fig18 <- fig18 + geom_text(data = outcome2,aes(x= row_cat, y = 82, label= index), hjust=0, vjust=-0.1) 

#Figure A24
fig18

####################################################################################################################
#
tapply(last2$difference, last2$complainant_gender, summary)
tapply(last2$difference, last2$gendered, summary)

##
fig19 <- ggplot(last2, aes(x=complainant_gender, y=difference)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 1600)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                  position=position_jitter(width=0.1, height=0.2)) 
fig19 <- fig19 + xlab("Complainant Gender") + ylab("Days") 
fig19 <- fig19 + theme_minimal() + ggtitle("Days in the Criminal Justice System (FIR to Recent Date in Judiciary)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
fig19

##
fig20 <- ggplot(last2, aes(x=gendered, y=difference)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 1600)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                  position=position_jitter(width=0.1, height=0.2)) 
fig20 <- fig20 + xlab("Crime Type") + ylab("Days") 
fig20 <- fig20 + theme_minimal() + ggtitle("Days in the Criminal Justice System (FIR to Recent Date in Judiciary)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
fig20

#
ggarrange(fig19, fig20, ncol=2, nrow=1, labels = c("A)", "B)"))
#Figure A15

table(last2$complainant_gender)

#############################################################################################################################
#
tapply(last3$difference, last3$complainant_gender, summary)
tapply(last3$difference, last3$gendered, summary) 

#
fig21 <- ggplot(last3, aes(x=complainant_gender, y=difference)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 1500)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                  position=position_jitter(width=0.1, height=0.2)) 
fig21 <- fig21 + xlab("Complainant Gender") + ylab("Days") 
fig21 <- fig21 + theme_minimal() + ggtitle("Days in the Criminal Justice System (FIR to Last Court Decision)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
fig21

#
fig22 <- ggplot(last3, aes(x=gendered, y=difference)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 1500)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                  position=position_jitter(width=0.1, height=0.2)) 
fig22 <- fig22 + xlab("Crime Type") + ylab("Days") 
fig22 <- fig22 + theme_minimal() + ggtitle("Days in the Criminal Justice System (FIR to Last Court Decision)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
fig22

#Figure A16
ggarrange(fig21, fig22, ncol=2, nrow=1, labels = c("A)", "B)"))


#############################################################################################################################
###
#
first$difference2 <- as.numeric(difftime(first$date_of_filing, first$system, units = c("days")))
sum(table(first$difference2))
summary(first$difference2, na.rm = TRUE)

#
table(first$difference2)
first$difference2 <- ifelse(first$difference2 < 0, NA, first$difference2)
summary(first$difference2)#

#
tapply(first$difference2, first$complainant_gender, summary)#

#
fig23 <- ggplot(first, aes(x=complainant_gender, y=difference2)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.2), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 300)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                 position=position_jitter(width=0.1, height=0.2)) 
fig23 <- fig23 + xlab("Complainant Gender") + ylab("Days") 
fig23 <- fig23 + theme_minimal() + ggtitle("Days in the Criminal Justice System (FIR to First Court Appearence)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
fig23

#
fig24 <- ggplot(first, aes(x=gendered, y=difference2)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.2), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 300)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                 position=position_jitter(width=0.1, height=0.2)) 
fig24 <- fig24 + xlab("Crime Type") + ylab("Days") 
fig24 <- fig24 + theme_minimal() + ggtitle("Days in the Criminal Justice System (FIR to First Court Appearence)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
fig24

#Figure A8
ggarrange(fig23, fig24, ncol=2, nrow=1, labels = c("A)", "B)"))

#######################
names(last2)
#
dat.m <- select(last2,difference, dowry, rape, fem_kidnapping, criminal_force)
dat.m$ident <- NULL 

names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:5)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)

tapply(dat.m$difference, dat.m$variable, summary)

fig25<- ggplot(dat.m, aes(x=variable, y=difference)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 1600)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                  position=position_jitter(width=0.1, height=0.2)) 
fig25 <- fig25 + xlab("Crime Type") + ylab("Days") 
fig25 <- fig25 + theme_minimal() + ggtitle("Days in the Criminal Justice System (FIR to Recent Date in Judiciary)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
fig25

###
names(last3)
#
dat.m <- select(last3,difference, dowry, rape, fem_kidnapping, criminal_force)
dat.m$ident <- NULL 

names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:5)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)

tapply(dat.m$difference, dat.m$variable, summary)

fig26<- ggplot(dat.m, aes(x=variable, y=difference)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=-0.4, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 1600)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                  position=position_jitter(width=0.1, height=0.2)) 
fig26 <- fig26 + xlab("Crime Type") + ylab("Days") 
fig26 <- fig26 + theme_minimal() + ggtitle("Days in the Criminal Justice System (FIR to Last Court Decision)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
fig26

#Figure A17
ggarrange(fig25, fig26, ncol=2, nrow=1, labels = c("A)", "B)"))


##
names(first)
#
dat.m <- select(first,difference2, dowry, rape, fem_kidnapping, criminal_force)
dat.m$ident <- NULL 

names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:5)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)


tapply(dat.m$difference2, dat.m$variable, summary)

fig27<- ggplot(dat.m, aes(x=variable, y=difference2)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=2, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 600)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                 position=position_jitter(width=0.1, height=0.2)) 
fig27 <- fig27 + xlab("Crime Type") + ylab("Days") 
fig27 <- fig27 + theme_minimal() + ggtitle("Days in the Criminal Justice System (FIR to First Court Appearence)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
#Figure A9
fig27


#################################################################################################################
##
names(last3)
#
dat.m <- select(last3,difference, 30:66)
dat.m$ident <- NULL 
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)#

tapply(dat.m$difference, dat.m$variable, summary)

#
dat.m$index <- ifelse(dat.m$variable == "ipc_379", "theft (N=28,187)", 
                      ifelse(dat.m$variable == "ipc_279", "rash driving (N=13,426)", 
                             ifelse(dat.m$variable == "ipc_380", "theft/burglary (N=8,665)", 
                                    ifelse(dat.m$variable == "ipc_457", "trespassing (N=5,848)", 
                                           ifelse(dat.m$variable == "ipc_420", "cheating (N=5,330)",
                                                  ifelse(dat.m$variable == "ipc_346", "wrongful confinement/missing person (N=3,981)",
                                                         ifelse(dat.m$variable == "ipc_304a", "culpable homicide (N=4,052)",
                                                                ifelse(dat.m$variable == "excise", "Excise Act/offenses related to liquor (N=30,009)",
                                                                       ifelse(dat.m$variable == "ipc_149", "unlawful assembly (N=5,975)",
                                                                              ifelse(dat.m$variable == "ipc_341", "wrongful restraint (N=5,348)",
                                                                                     ifelse(dat.m$variable == "ipc_148", "rioting (N=4,978)",
                                                                                            ifelse(dat.m$variable == "arms", "Arms Act/offenses related to weapons (N=5,494)",
                                                                                                   ifelse(dat.m$variable == "ipc_452", "trespassing at night (N=4,226)",
                                                                                                          ifelse(dat.m$variable == "ipc_365", "kidnapping and abducting (N=1,722)",
                                                                                                                 ifelse(dat.m$variable == "ipc_324", "causing hurt with weapons (N=2,920)",
                                                                                                                        ifelse(dat.m$variable == "electricity", "Electricity Act/offenses related to electricity theft (N=2,202)",
                                                                                                                               ifelse(dat.m$variable == "gambling", "Gambling/offenses related to gambling (N=12,148)",
                                                                                                                                      ifelse(dat.m$variable == "ipc_353", "assault on public servant (N=1,266)",
                                                                                                                                             ifelse(dat.m$variable == "ipc_363", "kidnapping (N=1,320)",
                                                                                                                                                    ifelse(dat.m$variable == "ipc_307", "attempt to murder (N=1,809)",
                                                                                                                                                           ifelse(dat.m$variable == "ipc_302", "murder (N=1,447)",
                                                                                                                                                                  ifelse(dat.m$variable == "narcotic", "Narcotics Act/offenses related to narcotics (N=2,973)",
                                                                                                                                                                         ifelse(dat.m$variable == "ipc_366a", "procuration of minor girl (N=893)",
                                                                                                                                                                                ifelse(dat.m$variable == "ipc_294", "obscene acts/songs (N=873)",
                                                                                                                                                                                       ifelse(dat.m$variable == "ipc_342", "wrongful confinement (N=719)",
                                                                                                                                                                                              ifelse(dat.m$variable == "ipc_392", "robbery (N=752)",
                                                                                                                                                                                                     ifelse(dat.m$variable == "ipc_306", "abetment of suicide (N=593)",
                                                                                                                                                                                                            ifelse(dat.m$variable == "ipc_510", "public misconduct by drunkard (N=773)",
                                                                                                                                                                                                                   ifelse(dat.m$variable == "public_property", "Damage to Public Property Act (N=1,356)",
                                                                                                                                                                                                                          ifelse(dat.m$variable == "animals", "Cruelty to Animals Act/offenses related to cattle/cow slaughter (N=2,238)",
                                                                                                                                                                                                                                 ifelse(dat.m$variable == "ipc_509", "word, gesture or act intended to insult modesty of a woman (N=223)",
                                                                                                                                                                                                                                        ifelse(dat.m$variable == "development", "Regulation of Urban Areas Act (N=167)",
                                                                                                                                                                                                                                               ifelse(dat.m$variable == "ipc_304b", "dowry death (N=437)",
                                                                                                                                                                                                                                                      ifelse(dat.m$variable == "ipc_354d", "stalking (N=212)",
                                                                                                                                                                                                                                                             ifelse(dat.m$variable == "ipc_354a", "sexual harassment (N=448)",
                                                                                                                                                                                                                                                                    ifelse(dat.m$variable == "scst", "Prevention of Atrocities Act/hate crime (N=545)","Protection of Children from Sexual Offenses Act (N=775)"))))))))))))))))))))))))))))))))))))
#
table(dat.m$variable)
table(dat.m$index)

##
fig28 <- ggplot(dat.m, aes(x=reorder(variable,difference,na.rm = TRUE), y=difference)) + geom_boxplot(outlier.colour = NA, notch=FALSE) + 
  stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(.5)), aes(label=sprintf("%1.1f", ..y..)),position=position_nudge(x=0), size=3, hjust=1.2) + 
  stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0), size=3, vjust=0.4, hjust=-1, aes(label=round(..y.., digits=1))) + 
  theme(legend.position = "none") + coord_flip() + xlab("Indian Penal Code/Special Act") + ylab("Days") + theme_minimal() + ggtitle("Days in the Criminal Justice System (FIR to Last Court Decision)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
fig28

last3_include <-dat.m %>% dplyr::group_by(variable) %>% dplyr::summarise(index = max(index))#

fig28 <- fig28 + geom_text(data = last3_include,aes(x= variable, y = 1000, label= index), hjust=0, vjust=-0.1) 
fig28##Figure A14


##
names(last2)

#
dat.m <- select(last2,difference, 30:66)
dat.m$ident <- NULL 
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)

tapply(dat.m$difference, dat.m$variable, summary)

#
dat.m$index <- ifelse(dat.m$variable == "ipc_379", "theft (N=38,888)", 
                      ifelse(dat.m$variable == "ipc_279", "rash driving (N=35,214)", 
                             ifelse(dat.m$variable == "ipc_380", "theft/burglary (N=13,578)", 
                                    ifelse(dat.m$variable == "ipc_457", "trespassing (N=8,906)", 
                                           ifelse(dat.m$variable == "ipc_420", "cheating (N=10,346)",
                                                  ifelse(dat.m$variable == "ipc_346", "wrongful confinement/missing person (N=4,364)",
                                                         ifelse(dat.m$variable == "ipc_304a", "culpable homicide (N=12,053)",
                                                                ifelse(dat.m$variable == "excise", "Excise Act/offenses related to liquor (N=37,679)",
                                                                       ifelse(dat.m$variable == "ipc_149", "unlawful assembly (N=12,426)",
                                                                              ifelse(dat.m$variable == "ipc_341", "wrongful restraint (N=10,446)",
                                                                                     ifelse(dat.m$variable == "ipc_148", "rioting (N=10,007)",
                                                                                            ifelse(dat.m$variable == "arms", "Arms Act/offenses related to weapons (N=11,493)",
                                                                                                   ifelse(dat.m$variable == "ipc_452", "trespassing at night (N=8,368)",
                                                                                                          ifelse(dat.m$variable == "ipc_365", "kidnapping and abducting (N=2,322)",
                                                                                                                 ifelse(dat.m$variable == "ipc_324", "causing hurt with weapons (N=5,440)",
                                                                                                                        ifelse(dat.m$variable == "electricity", "Electricity Act/offenses related to electricity theft (N=2,470)",
                                                                                                                               ifelse(dat.m$variable == "gambling", "Gambling/offenses related to gambling (N=13,795)",
                                                                                                                                      ifelse(dat.m$variable == "ipc_353", "assault on public servant (N=2,932)",
                                                                                                                                             ifelse(dat.m$variable == "ipc_363", "kidnapping (N=1,654)",
                                                                                                                                                    ifelse(dat.m$variable == "ipc_307", "attempt to murder (N=2,629)",
                                                                                                                                                           ifelse(dat.m$variable == "ipc_302", "murder (N=1,922)",
                                                                                                                                                                  ifelse(dat.m$variable == "narcotic", "Narcotics Act/offenses related to narcotics (N=7,022)",
                                                                                                                                                                         ifelse(dat.m$variable == "ipc_366a", "procuration of minor girl (N=1,143)",
                                                                                                                                                                                ifelse(dat.m$variable == "ipc_294", "obscene acts/songs (N=1,595)",
                                                                                                                                                                                       ifelse(dat.m$variable == "ipc_342", "wrongful confinement (N=1,350)",
                                                                                                                                                                                              ifelse(dat.m$variable == "ipc_392", "robbery (N=1,107)",
                                                                                                                                                                                                     ifelse(dat.m$variable == "ipc_306", "abetment of suicide (N=844)",
                                                                                                                                                                                                            ifelse(dat.m$variable == "ipc_510", "public misconduct by drunkard (N=1,224)",
                                                                                                                                                                                                                   ifelse(dat.m$variable == "public_property", "Damage to Public Property Act (N=3,949)",
                                                                                                                                                                                                                          ifelse(dat.m$variable == "animals", "Cruelty to Animals Act/offenses related to cattle/cow slaughter (N=3,338)",
                                                                                                                                                                                                                                 ifelse(dat.m$variable == "ipc_509", "word, gesture or act intended to insult modesty of a woman (N=559)",
                                                                                                                                                                                                                                        ifelse(dat.m$variable == "development", "Regulation of Urban Areas Act (N=586)",
                                                                                                                                                                                                                                               ifelse(dat.m$variable == "ipc_304b", "dowry death (N=542)",
                                                                                                                                                                                                                                                      ifelse(dat.m$variable == "ipc_354d", "stalking (N=443)",
                                                                                                                                                                                                                                                             ifelse(dat.m$variable == "ipc_354a", "sexual harassment (N=834)",
                                                                                                                                                                                                                                                                    ifelse(dat.m$variable == "scst", "Prevention of Atrocities Act/hate crime (N=814)","Protection of Children from Sexual Offenses Act (N=993)"))))))))))))))))))))))))))))))))))))
#
table(dat.m$variable)
table(dat.m$index)

##
fig29 <- ggplot(dat.m, aes(x=reorder(variable,difference,na.rm = TRUE), y=difference)) + geom_boxplot(outlier.colour = NA, notch=FALSE) + 
  stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(.5)), aes(label=sprintf("%1.1f", ..y..)),position=position_nudge(x=0), size=3, hjust=1.2) + 
  stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0), size=3, vjust=0.4, hjust=-1, aes(label=round(..y.., digits=1))) + 
  theme(legend.position = "none") + coord_flip() + xlab("Indian Penal Code/Special Act") + ylab("Days") + theme_minimal() + ggtitle("Days in the Criminal Justice System (FIR to Recent Date in Judicary)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
fig29

last2_include <-dat.m %>% dplyr::group_by(variable) %>% dplyr::summarise(index = max(index))#

fig29 <- fig29 + geom_text(data = last2_include,aes(x= variable, y = 1200, label= index), hjust=0, vjust=-0.1) 
fig29##Figure A13


###
names(first)
#
dat.m <- select(first,difference2, 30:66)
dat.m$ident <- NULL 
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)#


tapply(dat.m$difference2, dat.m$variable, summary)##


##
fig30 <- ggplot(dat.m, aes(x=reorder(variable,difference2,na.rm = TRUE), y=difference2)) + geom_boxplot(outlier.colour = NA, notch=FALSE) + 
  stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(.5)), aes(label=sprintf("%1.1f", ..y..)),position=position_nudge(x=0), size=3, hjust=1.2) + 
  stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0), size=3, vjust=0.4, hjust=-1, aes(label=round(..y.., digits=1))) + 
  theme(legend.position = "none") + coord_flip() + xlab("Indian Penal Code/Special Act") + ylab("Days") + theme_minimal() + ggtitle("Days in the Criminal Justice System (FIR to First Court Appearence)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
fig30

##
fig30 <- fig30 + geom_text(data = last2_include,aes(x= variable, y = 1200, label= index), hjust=0, vjust=-0.1) 
fig30##Figure A10

###################################################################################################################
#
names(last4)
last4$complainant_gender <- as.factor(last4$complainant_gender)
last4$gendered <- as.factor(last4$gendered)
da <- select(last4, complainant_gender, gendered, norecord)
da$ident <- NULL

##
descriptives1 <- xtable(summarize(da, type = "numeric", group = "complainant_gender"))
descriptives1 <- descriptives1[,c(1,2,4,6,7)]

###
test <- xtable(summarize(da))
test <- test[,c(1,2,4,5,9)]

##
descriptives2 <- xtable(summarize(da, type = "numeric", group = "gendered"))
names(descriptives2)
descriptives2 <- descriptives2[,c(1,2,4,6,7)]

##
names(descriptives1)[1] <- "variable"
descriptives2 <- select(descriptives2, 2:5)
descriptives <- cbind(descriptives1, descriptives2)

##
names(test)[1] <- "variable"
p <- plyr::join(descriptives, test, type='left', by='variable')
print(xtable(p), include.rownames=FALSE)


##
names(last2)
#
dat.m <- select(last2,filing_to_last, dowry, rape, fem_kidnapping, criminal_force)
dat.m$ident <- NULL

names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:5)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)

tapply(dat.m$filing_to_last, dat.m$variable, summary)

new<- ggplot(dat.m, aes(x=variable, y=filing_to_last)) + 
  geom_boxplot(outlier.colour = NA, notch=TRUE) + stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(0.25,.5,0.75)), aes(label=sprintf("%1.1f", ..y..)),
                                                               position=position_nudge(x=0.5), size=3.3) + stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0.5), size=3.2, vjust=2, aes(label=round(..y.., digits=1))) + theme(legend.position = "none") +
  coord_cartesian(ylim = c(0, 600)) + geom_point(size=0.08, shape=21, colour="gray52", alpha=0.2,
                                                 position=position_jitter(width=0.1, height=0.2)) 
new <- new + xlab("Crime Type") + ylab("Days") 
new <- new + theme_minimal() + ggtitle("Days in the Criminal Justice System (First Hearing to Most Recent Date)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
new##Figure A11


####
names(last2)
#
dat.m <- select(last2,filing_to_last, 30:66)
dat.m$ident <- NULL 
names(dat.m)
dat.m <- reshape2::melt(dat.m, measure.vars=2:38)

#
dat.m <- dat.m[with(dat.m, grepl(1, dat.m$value)),]
table(dat.m$variable)#

tapply(dat.m$filing_to_last, dat.m$variable, summary)#
               

table(dat.m$variable)

##
new <- ggplot(dat.m, aes(x=reorder(variable,filing_to_last,na.rm = TRUE), y=filing_to_last)) + geom_boxplot(outlier.colour = NA, notch=FALSE) + 
  stat_summary(geom="text", fun = quantile, fun.args=list(probs=c(.5)), aes(label=sprintf("%1.1f", ..y..)),position=position_nudge(x=0), size=3, hjust=1.2) + 
  stat_summary(fun=mean, colour="red", geom="text", position=position_nudge(x=0), size=3, vjust=0.4, hjust=-1, aes(label=round(..y.., digits=1))) + 
  theme(legend.position = "none") + coord_flip() + xlab("Indian Penal Code/Special Act") + ylab("Days") + theme_minimal() + ggtitle("Days in Court (First to Most Recent Hearing)") + theme(plot.title = element_text(size = 15, hjust=0.5)) + theme(text = element_text(size = 20))
new

new <- new + geom_text(data = last2_include,aes(x= variable, y = 1200, label= index), hjust=0, vjust=-0.1) 
new##Figure A12

##
data5 <- select(last2, complainant_gender, gendered, acquitted, convicted, dismissed, ongoing, difference, filing_to_last, no_of_hearings, judge_rank)
data5$ident <- NULL
data5 <- data.frame(data5)
data5$complainant_gender <- as.factor(data5$complainant_gender)
data5$gendered <- as.factor(data5$gendered)

##
data5 <- dummy_cols(data5, select_columns = "judge_rank")
descriptives1 <- xtable(summarize(data5, type = "numeric", group = "complainant_gender"))
names(descriptives1)
descriptives1 <- descriptives1[,c(1,2,4,7,8)]

##
test <- xtable(summarize(data5))
test <- test[,c(1,2,5,6,10)]
print(xtable(test), include.rownames=FALSE)

##
descriptives2 <- xtable(summarize(data5, type = "numeric", group = "gendered"))
names(descriptives2)
descriptives2 <- descriptives2[,c(1,2,4,7,8)]

##
names(descriptives1)[1] <- "variable"
descriptives2 <- select(descriptives2, 2:5)
descriptives <- cbind(descriptives1, descriptives2)

##
##Table 2
names(test)[1] <- "variable"
p <- plyr::join(descriptives, test, type='left', by='variable')
print(xtable(p), include.rownames=FALSE)

###
###
data5 <- select(first, complainant_gender, gendered, difference2)
data5$ident <- NULL
data5$complainant_gender <- as.factor(data5$complainant_gender)
data5$gendered <- as.factor(data5$gendered)

descriptives1 <- xtable(summarize(data5, type = "numeric", group = "complainant_gender"))
names(descriptives1)
descriptives1 <- descriptives1[,c(1,2,4,7,8)]

###
test <- xtable(summarize(data5))
test <- test[,c(1,2,5,6,10)]

##
descriptives2 <- xtable(summarize(data5, type = "numeric", group = "gendered"))
names(descriptives2)
descriptives2 <- descriptives2[,c(1,2,4,7,8)]

##
names(descriptives1)[1] <- "variable"
descriptives2 <- select(descriptives2, 2:5)
descriptives <- cbind(descriptives1, descriptives2)

##
names(test)[1] <- "variable"
p <- plyr::join(descriptives, test, type='left', by='variable')
print(xtable(p), include.rownames=FALSE)


##########################
names(last4)

#
rreg0 <- lm(norecord~complainant_fem, data = last4)
vcov_district <- cluster.vcov(rreg0, last4$district)
rreg0 <- coeftest(rreg0, vcov_district)


rreg3 <- lm(norecord~complainant_fem + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(rreg3, last4$district)
rreg3 <- coeftest(rreg3, vcov_district)

#
rreg00 <- lm(norecord~gendered, data = last4)
vcov_district <- cluster.vcov(rreg00, last4$district)
rreg00 <- coeftest(rreg00, vcov_district)

rreg15 <- lm(norecord~gendered + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(rreg15, last4$district)
rreg15 <- coeftest(rreg15, vcov_district)

#
rreg000 <- lm(norecord~complainant_fem*gendered, data = last4)
vcov_district <- cluster.vcov(rreg000, last4$district)
rreg000 <- coeftest(rreg000, vcov_district)
rreg000

rreg27 <- lm(norecord~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(rreg27, last4$district)
rreg27 <- coeftest(rreg27, vcov_district)

##Table 3, columns 7-12
#stargazer(reg0, reg3, reg15, reg000, reg27, rreg0, rreg3, rreg15, rreg000, rreg27, type="text", align = FALSE,omit.stat=c("adj.rsq","ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","distance","ps_category"))
stargazer(rreg0, rreg3, rreg00, rreg15, rreg000, rreg27, type="text", align = FALSE,omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","distance","ps_category"))
#stargazer(rreg0, rreg3, rreg00, rreg15, rreg000, rreg27, align = FALSE,omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","distance","ps_category"))
#stargazer(rreg0, rreg3, rreg00, rreg15, rreg000, rreg27, align = FALSE, omit.stat=c("ser","f"), out="filename.tex")
#stargazer(reg0, reg3, reg00, reg15, reg000, reg27,rreg0, rreg3, rreg00, rreg15, rreg000, rreg27, align = FALSE,omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","distance","ps_category"))
#stargazer(reg0, reg3, reg00, reg15, reg000, reg27,rreg0, rreg3, rreg00, rreg15, rreg000, rreg27, type="text", align = FALSE,omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","distance","ps_category"))



##
rreg27 <- lm(norecord~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance , data = last4)
int1 <- ggeffect(rreg27, c("gendered", "complainant_fem"))
int1
int1 <- ggplot(int1, aes(x=x, y=predicted, colour = group, group=group)) +
  geom_point() + geom_line() + geom_errorbar(aes(ymin = conf.low, ymax = conf.high, width=0.05)) +
  scale_x_discrete(breaks = 0:1, expand = expansion(mult = c(0.2, 0.2)), labels=c("0" = "Non-VAW", "1" = "VAW")) + scale_color_manual(labels = c("Male", "Female"), values = c("blue", "red")) +
  theme(axis.ticks=element_blank(), axis.title.x=element_blank(), legend.title=element_blank()) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "grey20")) +
  theme(text = element_text(size = 20)) +theme(legend.position="bottom") +
  geom_text(aes(label = round(predicted,3)),vjust=2, hjust=1.3) +
  ggtitle("No Record in Court/Cancelled at PS") + ylab("Percent") + theme(plot.title = element_text(size = 20, hjust=0.5)) 
int1


###############
##
eg0 <- lm(difference2~complainant_fem, data = first)
vcov_district <- cluster.vcov(eg0, first$district)
eg0 <- coeftest(eg0, vcov_district)

eg3 <- lm(difference2~complainant_fem  + my_station + Month_Yr + io_rank + distance  + judge_rank, data = first)
vcov_district <- cluster.vcov(eg3, first$district)
eg3 <- coeftest(eg3, vcov_district)

##
eg00 <- lm(difference2~gendered, data = first)
vcov_district <- cluster.vcov(eg00, first$district)
eg00 <- coeftest(eg00, vcov_district)

eg15 <- lm(difference2~gendered  + my_station + Month_Yr + io_rank + distance  + judge_rank, data = first)
vcov_district <- cluster.vcov(eg15, first$district)
eg15 <- coeftest(eg15, vcov_district)

##
eg000 <- lm(difference2~complainant_fem*gendered, data = first)
vcov_district <- cluster.vcov(eg000, first$district)
eg000 <- coeftest(eg000, vcov_district)


eg27 <- lm(difference2~complainant_fem*gendered  + my_station + Month_Yr + io_rank + distance  + judge_rank, data = first)
vcov_district <- cluster.vcov(eg27, first$district)
eg27 <- coeftest(eg27, vcov_district)

#Table 4, columns 1-6
stargazer(eg0, eg3,eg00, eg15, eg000, eg27, align = FALSE, type="text", omit.stat=c("adj.rsq","ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01", "distance", "ps_category", "judge_rank"))
stargazer(eg0, eg3,eg00, eg15, eg000, eg27, align = FALSE, omit.stat=c("adj.rsq","ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01", "distance", "ps_category", "judge_rank"))

##
eg27 <- lm(difference2~complainant_fem*gendered  + my_station + Month_Yr + io_rank + distance  + judge_rank, data = first)
int2 <- ggeffect(eg27, c("gendered", "complainant_fem"))
int2
int2 <- ggplot(int2, aes(x=x, y=predicted, colour = group, group=group)) +
  geom_point() + geom_line() + geom_errorbar(aes(ymin = conf.low, ymax = conf.high, width=0.05)) +
  scale_x_discrete(breaks = 0:1, expand = expansion(mult = c(0.2, 0.2)), labels=c("0" = "Non-VAW", "1" = "VAW")) + scale_color_manual(labels = c("Male", "Female"), values = c("blue", "red")) +
  theme(axis.ticks=element_blank(), axis.title.x=element_blank(), legend.title=element_blank()) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "grey20")) +
  theme(text = element_text(size = 20)) +theme(legend.position="bottom") +
  geom_text(aes(label = round(predicted,2)), vjust=-2, hjust=1.3) +
  ggtitle("Investigation Duration") + ylab("Days") + theme(plot.title = element_text(size = 20, hjust=0.5)) 
int2



##
g0 <- lm(dismissed~complainant_fem, data = last2)
vcov_district <- cluster.vcov(g0, last2$district)
g0 <- coeftest(g0, vcov_district)

g3 <- lm(dismissed~complainant_fem + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(g3, last2$district)
g3 <- coeftest(g3, vcov_district)

##
g00 <- lm(dismissed~gendered, data = last2)
vcov_district <- cluster.vcov(g00, last2$district)
g00 <- coeftest(g00, vcov_district)

g15 <- lm(dismissed~gendered + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(g15, last2$district)
g15 <- coeftest(g15, vcov_district)

##
g000 <- lm(dismissed~complainant_fem*gendered, data = last2)
vcov_district <- cluster.vcov(g000, last2$district)
g000 <- coeftest(g000, vcov_district)

g27 <- lm(dismissed~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(g27, last2$district)
g27 <- coeftest(g27, vcov_district)

#Table 4, column 7-12
stargazer(g0, g3, g00, g15, g000, g27, align = FALSE,type="text", omit.stat=c("adj.rsq","ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01", "judge_rank", "distance", "ps_category"))
stargazer(g0, g3, g00, g15, g000, g27, align = FALSE, omit.stat=c("adj.rsq","ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01", "judge_rank", "distance", "ps_category"))

##
g27 <- lm(dismissed~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
int3 <- ggeffect(g27, c("gendered", "complainant_fem"))
int3
int3 <- ggplot(int3, aes(x=x, y=predicted, colour = group, group=group)) +
  geom_point() + geom_line() + geom_errorbar(aes(ymin = conf.low, ymax = conf.high, width=0.05)) +
  scale_x_discrete(breaks = 0:1, expand = expansion(mult = c(0.2, 0.2)), labels=c("0" = "Non-VAW", "1" = "VAW")) + scale_color_manual(labels = c("Male", "Female"), values = c("blue", "red")) +
  theme(axis.ticks=element_blank(), axis.title.x=element_blank(), legend.title=element_blank()) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "grey20")) +
  theme(text = element_text(size = 20)) +theme(legend.position="bottom") +
  geom_text(aes(label = round(predicted,3)),vjust=-0.2, hjust=1.3) +
  ggtitle("Court/Preliminary Dismissal") + ylab("Percent") + theme(plot.title = element_text(size = 20, hjust=0.5)) 
int3

#
#stargazer(eg0, eg3,eg00, eg15, eg000, eg27,g0, g3, g00, g15, g000, g27, type="text", align = FALSE,omit.stat=c("ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01", "judge_rank", "distance", "ps_category"))
#stargazer(eg0, eg3,eg00, eg15, eg000, eg27,g0, g3, g00, g15, g000, g27, align = FALSE,omit.stat=c("ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01", "judge_rank", "distance", "ps_category"))
#stargazer(eg0, eg3,eg00, eg15, eg000, eg27,g0, g3, g00, g15, g000, g27, align = FALSE, omit.stat=c("ser","f"), out="filename.tex")



##
x0 <- lm(filing_to_last~complainant_fem, data = last2)
vcov_district <- cluster.vcov(x0, last2$district)
x0 <- coeftest(x0, vcov_district)

x3 <- lm(filing_to_last~complainant_fem + as.factor(my_station) + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(x3, last2$district)
x3 <- coeftest(x3, vcov_district)

##
x00 <- lm(filing_to_last~gendered, data = last2)
vcov_district <- cluster.vcov(x00, last2$district)
x00 <- coeftest(x00, vcov_district)

x15 <- lm(filing_to_last~gendered + as.factor(my_station) + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(x15, last2$district)
x15 <- coeftest(x15, vcov_district)


##
x000 <- lm(filing_to_last~complainant_fem*gendered, data = last2)
vcov_district <- cluster.vcov(x000, last2$district)
x000 <- coeftest(x000, vcov_district)


x27 <- lm(filing_to_last~complainant_fem*gendered + as.factor(my_station) + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(x27, last2$district)
x27 <- coeftest(x27, vcov_district)
x27

#Table 5
stargazer(x0, x3, x00, x15, x000, x27, align = FALSE,type="text", omit.stat=c("ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01","judge_rank","distance"))
stargazer(x0, x3, x00, x15, x000, x27, align = FALSE,omit.stat=c("ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01","judge_rank", "ps_category","distance"))
#stargazer(x0, x3, x00, x15, x000, x27, align = FALSE, omit.stat=c("ser","f"), out="filename.tex")


##
x27 <- lm(filing_to_last~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
int4 <- ggeffect(x27, c("gendered", "complainant_fem"))
int4
int4 <- ggplot(int4, aes(x=x, y=predicted, colour = group, group=group)) +
  geom_point() + geom_line() + geom_errorbar(aes(ymin = conf.low, ymax = conf.high, width=0.05)) +
  scale_x_discrete(breaks = 0:1, expand = expansion(mult = c(0.2, 0.2)), labels=c("0" = "Non-VAW", "1" = "VAW")) + scale_color_manual(labels = c("Male", "Female"), values = c("blue", "red")) +
  theme(axis.ticks=element_blank(), axis.title.x=element_blank(), legend.title=element_blank()) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "grey20")) +
  theme(text = element_text(size = 20)) +theme(legend.position="bottom") +
  geom_text(aes(label = round(predicted,2)), vjust=-0.2, hjust=1.2) +
  ggtitle("Duration in Court") + ylab("Days") + theme(plot.title = element_text(size = 20, hjust=0.5)) 
int4


##
y0 <- lm(convicted~complainant_fem, data = last2)
vcov_district <- cluster.vcov(y0, last2$district)
y0 <- coeftest(y0, vcov_district)

y3 <- lm(convicted~complainant_fem + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(y3, last2$district)
y3 <- coeftest(y3, vcov_district)

##
y00 <- lm(convicted~gendered, data = last2)
vcov_district <- cluster.vcov(y00, last2$district)
y00 <- coeftest(y00, vcov_district)

y15 <- lm(convicted~gendered + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(y15, last2$district)
y15 <- coeftest(y15, vcov_district)


##
y000 <- lm(convicted~complainant_fem*gendered, data = last2)
vcov_district <- cluster.vcov(y000, last2$district)
y000 <- coeftest(y000, vcov_district)


y27 <- lm(convicted~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(y27, last2$district)
y27 <- coeftest(y27, vcov_district)

#Table 6, columns 7-12
stargazer(y0, y3, y00, y15, y000, y27, align = FALSE,type="text",omit.stat=c("ser","f"), omit = c("station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance"))
stargazer(y0, y3, y00, y15, y000, y27, align = FALSE)
#stargazer(y0, y3, y00, y15, y000, y27, align = FALSE, out="filename.tex")

##
y27 <- lm(convicted~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
int5 <- ggeffect(y27, c("gendered", "complainant_fem"))
int5
int5 <- ggplot(int5, aes(x=x, y=predicted, colour = group, group=group)) +
  geom_point() + geom_line() + geom_errorbar(aes(ymin = conf.low, ymax = conf.high, width=0.05)) +
  scale_x_discrete(breaks = 0:1, expand = expansion(mult = c(0.2, 0.2)), labels=c("0" = "Non-VAW", "1" = "VAW")) + scale_color_manual(labels = c("Male", "Female"), values = c("blue", "red")) +
  theme(axis.ticks=element_blank(), axis.title.x=element_blank(), legend.title=element_blank()) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "grey20")) +
  theme(text = element_text(size = 20)) +theme(legend.position="bottom") +
  geom_text(aes(label = round(predicted,3)), vjust=-0.2, hjust=1.4) +
  ggtitle("Conviction") + ylab("Percent") + theme(plot.title = element_text(size = 20, hjust=0.5)) 
int5

##Figure 4
final <- ggarrange(int0, int1, int2, int3, int4, int5, nrow=2, ncol=3, common.legend = TRUE, legend="bottom", labels = c("A)", "B)","C)","D)","E)","F)"))
final

############################################################################
#######Acquittal
last2$acquitted <- ifelse(grepl("acquitted", last2$my_court_status2), 1, 0)

##
z0 <- lm(acquitted~complainant_fem, data = last2)
vcov_district <- cluster.vcov(z0, last2$district)
z0 <- coeftest(z0, vcov_district)

z3 <- lm(acquitted~complainant_fem + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(z3, last2$district)
z3 <- coeftest(z3, vcov_district)

##
z00 <- lm(acquitted~gendered, data = last2)
vcov_district <- cluster.vcov(z00, last2$district)
z00 <- coeftest(z00, vcov_district)

z15 <- lm(acquitted~gendered + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(z15, last2$district)
z15 <- coeftest(z15, vcov_district)

##
z000 <- lm(acquitted~complainant_fem*gendered, data = last2)
vcov_district <- cluster.vcov(z000, last2$district)
z000 <- coeftest(z000, vcov_district)


z27 <- lm(acquitted~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(z27, last2$district)
z27 <- coeftest(z27, vcov_district)

#Table 6, columns 1-6
stargazer(z0, z3, z00, z15, z000, z27, align = FALSE,type="text", omit.stat=c("adj.rsq","ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank", "ps_category","distance","station"))
stargazer(z0, z3, z00, z15, z000, z27, align = FALSE, omit.stat=c("adj.rsq","ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank", "ps_category","distance","station"))

##
##stargazer(z0, z3, z00, z15, z000, z27, y0, y3, y00, y15, y000, y27, type="text", align = FALSE,omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance","station"))
##stargazer(z0, z3, z00, z15, z000, z27, y0, y3, y00, y15, y000, y27, align = FALSE,omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance","station"))
#stargazer(z0, z3, z00, z15, z000, z27, y0, y3, y00, y15, y000, y27, align = FALSE, omit.stat=c("ser","f"), out="filename.tex")




###Primary PC FE
#unique(last4$sections_01)
#unique(last4$id)
reg4 <- lm(diff_in_days~complainant_fem  + my_station + Month_Yr + io_rank + distance  + as.factor(id), data = last4)
vcov_district <- cluster.vcov(reg4, last4$district)
reg4 <- coeftest(reg4, vcov_district)

rreg4 <- lm(norecord~complainant_fem + my_station + Month_Yr + io_rank + distance  + as.factor(id), data = last4)
vcov_district <- cluster.vcov(rreg4, last4$district)
rreg4 <- coeftest(rreg4, vcov_district)

eg4 <- lm(difference2~complainant_fem  + my_station + Month_Yr + io_rank + distance  + judge_rank + as.factor(id), data = first)
vcov_district <- cluster.vcov(eg4, first$district)
eg4 <- coeftest(eg4, vcov_district)

g4 <- lm(dismissed~complainant_fem + my_station + Month_Yr + io_rank + distance  + judge_rank + as.factor(id), data = last2)
vcov_district <- cluster.vcov(g4, last2$district)
g4 <- coeftest(g4, vcov_district)

x4 <- lm(filing_to_last~complainant_fem + my_station + Month_Yr + io_rank + distance  + judge_rank + as.factor(id), data = last2)
vcov_district <- cluster.vcov(x4, last2$district)
x4 <- coeftest(x4, vcov_district)

y4 <- lm(convicted~complainant_fem + my_station + Month_Yr + io_rank + distance  + judge_rank + as.factor(id), data = last2)
vcov_district <- cluster.vcov(y4, last2$district)
y4 <- coeftest(y4, vcov_district)

z4 <- lm(acquitted~complainant_fem + my_station + Month_Yr + io_rank + distance  + judge_rank + as.factor(id), data = last2)
vcov_district <- cluster.vcov(z4, last2$district)
z4 <- coeftest(z4, vcov_district)

##Table A2
stargazer(reg4, rreg4, eg4, g4, x4, z4,  y4, align = FALSE,type="text", omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank","judge_rank","ps_category","distance", "station", "id", "sections_01"))
stargazer(reg4, rreg4, eg4, g4, x4, z4,  y4, align = FALSE, omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank","judge_rank","ps_category","distance", "station", "id", "sections_01"))
#stargazer(reg4, rreg4, eg4, g4, x4, z4,  y4, align = FALSE, out="filename.tex")


#########################
heter1 <- lm(norecord~dowry  + rape + fem_kidnapping + criminal_force, data = last4)
vcov_district <- cluster.vcov(heter1, last4$district)
heter1 <- coeftest(heter1, vcov_district)

heter2 <- lm(norecord~dowry  + rape + fem_kidnapping + criminal_force + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(heter2, last4$district)
heter2 <- coeftest(heter2, vcov_district)

heter3 <- lm(norecord~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force, data = last4)
vcov_district <- cluster.vcov(heter3, last4$district)
heter3 <- coeftest(heter3, vcov_district)

heter4 <- lm(norecord~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(heter4, last4$district)
heter4 <- coeftest(heter4, vcov_district)

##Table A4
stargazer(heter1, heter2, heter3, heter4, align = FALSE, type="text", omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance"))
stargazer(heter1, heter2, heter3, heter4, align = FALSE, omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance"))
#stargazer(heter1, heter2, heter3, heter4, align = FALSE, out="filename.tex")

##
heter5 <- lm(difference2~dowry  + rape + fem_kidnapping + criminal_force, data = first)
vcov_district <- cluster.vcov(heter5, first$district)
heter5 <- coeftest(heter5, vcov_district)

heter6 <- lm(difference2~dowry  + rape + fem_kidnapping + criminal_force + my_station + Month_Yr + io_rank + distance  + judge_rank, data = first)
vcov_district <- cluster.vcov(heter6, first$district)
heter6 <- coeftest(heter6, vcov_district)

heter7 <- lm(difference2~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force, data = first)
vcov_district <- cluster.vcov(heter7, first$district)
heter7 <- coeftest(heter7, vcov_district)

heter8 <- lm(difference2~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force + my_station + Month_Yr + io_rank + distance  + judge_rank, data = first)
vcov_district <- cluster.vcov(heter8, first$district)
heter8 <- coeftest(heter8, vcov_district)

##
heter9 <- lm(dismissed~dowry  + rape + fem_kidnapping + criminal_force, data = last2)
vcov_district <- cluster.vcov(heter9, last2$district)
heter9 <- coeftest(heter9, vcov_district)

heter10 <- lm(dismissed~dowry  + rape + fem_kidnapping + criminal_force + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(heter10, last2$district)
heter10 <- coeftest(heter10, vcov_district)

heter11 <- lm(dismissed~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force, data = last2)
vcov_district <- cluster.vcov(heter11, last2$district)
heter11 <- coeftest(heter11, vcov_district)

heter12 <- lm(dismissed~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(heter12, last2$district)
heter12 <- coeftest(heter12, vcov_district)

##Table A5
stargazer(heter5, heter6, heter7, heter8, align = FALSE, type="text", omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance"))
stargazer(heter9, heter10, heter11, heter12, align = FALSE, type="text", omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance"))
#stargazer(heter5, heter6, heter7, heter8, heter9, heter10, heter11, heter12, align = FALSE, out="filename.tex")

##
heter13 <- lm(filing_to_last~dowry  + rape + fem_kidnapping + criminal_force, data = last2)
vcov_district <- cluster.vcov(heter13, last2$district)
heter13 <- coeftest(heter13, vcov_district)

heter14 <- lm(filing_to_last~dowry  + rape + fem_kidnapping + criminal_force + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(heter14, last2$district)
heter14 <- coeftest(heter14, vcov_district)

heter15 <- lm(filing_to_last~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force, data = last2)
vcov_district <- cluster.vcov(heter15, last2$district)
heter15 <- coeftest(heter15, vcov_district)

heter16 <- lm(filing_to_last~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force  + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(heter16, last2$district)
heter16 <- coeftest(heter16, vcov_district)

##
heter17 <- lm(acquitted~dowry  + rape + fem_kidnapping + criminal_force, data = last2)
vcov_district <- cluster.vcov(heter17, last2$district)
heter17 <- coeftest(heter17, vcov_district)

heter18 <- lm(acquitted~dowry  + rape + fem_kidnapping + criminal_force + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(heter18, last2$district)
heter18 <- coeftest(heter18, vcov_district)

heter19 <- lm(acquitted~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force, data = last2)
vcov_district <- cluster.vcov(heter19, last2$district)
heter19 <- coeftest(heter19, vcov_district)

heter20 <- lm(acquitted~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(heter20, last2$district)
heter20 <- coeftest(heter20, vcov_district)

##
heter21 <- lm(convicted~dowry  + rape + fem_kidnapping + criminal_force, data = last2)
vcov_district <- cluster.vcov(heter21, last2$district)
heter21 <- coeftest(heter21, vcov_district)

heter22 <- lm(convicted~dowry  + rape + fem_kidnapping + criminal_force + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(heter22, last2$district)
heter22 <- coeftest(heter22, vcov_district)

heter23 <- lm(convicted~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force, data = last2)
vcov_district <- cluster.vcov(heter23, last2$district)
heter23 <- coeftest(heter23, vcov_district)

heter24 <- lm(convicted~complainant_fem*dowry  + complainant_fem*rape + complainant_fem*fem_kidnapping + complainant_fem*criminal_force + my_station + Month_Yr + io_rank + distance  + judge_rank, data = last2)
vcov_district <- cluster.vcov(heter24, last2$district)
heter24 <- coeftest(heter24, vcov_district)

##Table A6
stargazer(heter13, heter14, heter15, heter16, align = FALSE, type="text", omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance"))
stargazer(heter17, heter18, heter19, heter20, align = FALSE, type="text", omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance"))
stargazer(heter21, heter22, heter23, heter24, align = FALSE, type="text", omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance"))
stargazer(heter13, heter14, heter15, heter16, heter17, heter18, heter19, heter20, heter21, heter22, heter23, heter24, align = FALSE, type="text", omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance"))
stargazer(heter13, heter14, heter15, heter16, heter17, heter18, heter19, heter20, heter21, heter22, heter23, heter24, align = FALSE, omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank","ps_category","distance"))
#stargazer(heter13, heter14, heter15, heter16, heter17, heter18, heter19, heter20, heter21, heter22, heter23, heter24, align = FALSE, out="filename.tex")


####All 
##
last4$dismissed <- ifelse(grepl("dismissed_cancelled", last4$my_court_status2), 1, 0)

##
g0 <- lm(dismissed~complainant_fem, data = last4)
vcov_district <- cluster.vcov(g0, last4$district)
g0 <- coeftest(g0, vcov_district)

g3 <- lm(dismissed~complainant_fem + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(g3, last4$district)
g3 <- coeftest(g3, vcov_district)

##
g00 <- lm(dismissed~gendered, data = last4)
vcov_district <- cluster.vcov(g00, last4$district)
g00 <- coeftest(g00, vcov_district)

g15 <- lm(dismissed~gendered + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(g15, last4$district)
g15 <- coeftest(g15, vcov_district)

##
g000 <- lm(dismissed~complainant_fem*gendered, data = last4)
vcov_district <- cluster.vcov(g000, last4$district)
g000 <- coeftest(g000, vcov_district)


g27 <- lm(dismissed~complainant_fem*gendered+ my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(g27, last4$district)
g27 <- coeftest(g27, vcov_district)


##Table A7
stargazer(g0, g3, g00, g15, g000, g27, align = FALSE, type="text", omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","distance","ps_category","judge_rank"))
stargazer(g0, g3, g00, g15, g000, g27, align = FALSE, omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","distance","ps_category","judge_rank"))
#stargazer(g0, g3, g00, g15, g000, g27, align = FALSE, out="filename.tex")


##
last4$convicted <- ifelse(grepl("convicted", last4$my_court_status2), 1, 0)
last4$acquitted <- ifelse(grepl("acquitted", last4$my_court_status2), 1, 0)

##
y0 <- lm(convicted~complainant_fem, data = last4)
vcov_district <- cluster.vcov(y0, last4$district)
y0 <- coeftest(y0, vcov_district)

y3 <- lm(convicted~complainant_fem + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(y3, last4$district)
y3 <- coeftest(y3, vcov_district)


##
y00 <- lm(convicted~gendered, data = last4)
vcov_district <- cluster.vcov(y00, last4$district)
y00 <- coeftest(y00, vcov_district)

y15 <- lm(convicted~gendered + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(y15, last4$district)
y15 <- coeftest(y15, vcov_district)


##
y000 <- lm(convicted~complainant_fem*gendered, data = last4)
vcov_district <- cluster.vcov(y000, last4$district)
y000 <- coeftest(y000, vcov_district)

y27 <- lm(convicted~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(y27, last4$district)
y27 <- coeftest(y27, vcov_district)

##
z0 <- lm(acquitted~complainant_fem, data = last4)
vcov_district <- cluster.vcov(z0, last4$district)
z0 <- coeftest(z0, vcov_district)

z3 <- lm(acquitted~complainant_fem + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(z3, last4$district)
z3 <- coeftest(z3, vcov_district)

##
z00 <- lm(acquitted~gendered, data = last4)
vcov_district <- cluster.vcov(z00, last4$district)
z00 <- coeftest(z00, vcov_district)

z15 <- lm(acquitted~gendered + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(z15, last4$district)
z15 <- coeftest(z15, vcov_district)

##
z000 <- lm(acquitted~complainant_fem*gendered, data = last4)
vcov_district <- cluster.vcov(z000, last4$district)
z000 <- coeftest(z000, vcov_district)


z27 <- lm(acquitted~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance , data = last4)
vcov_district <- cluster.vcov(z27, last4$district)
z27 <- coeftest(z27, vcov_district)

##Table A8
stargazer(z0, z3, z00, z15, z000, z27, y0, y3, y00, y15, y000, y27, align = FALSE,type="text", omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank", "ps_category","distance","station"))
stargazer(z0, z3, z00, z15, z000, z27, y0, y3, y00, y15, y000, y27, align = FALSE,omit.stat=c("ser","f"), omit = c("my_station","Month_Yr","io_rank", "sections_01","judge_rank", "ps_category","distance","station"))
#stargazer(z0, z3, z00, z15, z000, z27, y0, y3, y00, y15, y000, y27, align = FALSE, out="filename.tex")

##
g27 <- lm(dismissed~complainant_fem*gendered+ my_station + Month_Yr + io_rank + distance , data = last4)
int3 <- ggeffect(g27, c("gendered", "complainant_fem"))
int3
int3 <- ggplot(int3, aes(x=x, y=predicted, colour = group, group=group)) +
  geom_point() + geom_line() + geom_errorbar(aes(ymin = conf.low, ymax = conf.high, width=0.05)) +
  scale_x_discrete(breaks = 0:1, expand = expansion(mult = c(0.2, 0.2)), labels=c("0" = "Non-VAW", "1" = "VAW")) + scale_color_manual(labels = c("Male", "Female"), values = c("blue", "red")) +
  theme(axis.ticks=element_blank(), axis.title.x=element_blank(), legend.title=element_blank()) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "grey20")) +
  theme(text = element_text(size = 20)) +theme(legend.position="bottom") +
  geom_text(aes(label = round(predicted,3)),vjust=-0.2, hjust=1.3) +
  ggtitle("Court/Preliminary Dismissal") + ylab("Percent") + theme(plot.title = element_text(size = 20, hjust=0.5)) 
int3

##
y27 <- lm(convicted~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance , data = last4)
int5 <- ggeffect(y27, c("gendered", "complainant_fem"))
int5
int5 <- ggplot(int5, aes(x=x, y=predicted, colour = group, group=group)) +
  geom_point() + geom_line() + geom_errorbar(aes(ymin = conf.low, ymax = conf.high, width=0.05)) +
  scale_x_discrete(breaks = 0:1, expand = expansion(mult = c(0.2, 0.2)), labels=c("0" = "Non-VAW", "1" = "VAW")) + scale_color_manual(labels = c("Male", "Female"), values = c("blue", "red")) +
  theme(axis.ticks=element_blank(), axis.title.x=element_blank(), legend.title=element_blank()) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "grey20")) +
  theme(text = element_text(size = 20)) +theme(legend.position="bottom") +
  geom_text(aes(label = round(predicted,3)), vjust=-0.2, hjust=1.4) +
  ggtitle("Conviction") + ylab("Percent") + theme(plot.title = element_text(size = 20, hjust=0.5)) 
int5

##
z27 <- lm(acquitted~complainant_fem*gendered + my_station + Month_Yr + io_rank + distance , data = last4)
int6 <- ggeffect(z27, c("gendered", "complainant_fem"))
int6
int6 <- ggplot(int6, aes(x=x, y=predicted, colour = group, group=group)) +
  geom_point() + geom_line() + geom_errorbar(aes(ymin = conf.low, ymax = conf.high, width=0.05)) +
  scale_x_discrete(breaks = 0:1, expand = expansion(mult = c(0.2, 0.2)), labels=c("0" = "Non-VAW", "1" = "VAW")) + scale_color_manual(labels = c("Male", "Female"), values = c("blue", "red")) +
  theme(axis.ticks=element_blank(), axis.title.x=element_blank(), legend.title=element_blank()) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "grey20")) +
  theme(text = element_text(size = 20)) +theme(legend.position="bottom") +
  geom_text(aes(label = round(predicted,3)), vjust=-0.2, hjust=1.4) +
  ggtitle("Acquittal") + ylab("Percent") + theme(plot.title = element_text(size = 20, hjust=0.5)) 
int6

##Figure A27
final <- ggarrange(int3, int6, int5, nrow=1, ncol=3, common.legend = TRUE, legend="bottom", labels = c("A)", "B)","C)"))
final
